zoukankan      html  css  js  c++  java
  • 单链表逆序

    非常简单,就当练个手吧

     1 //单链表逆序问题,其实很容易的,就是把指针指向给变一下,注意的几个问题
     2 //(1)如果就一个元素,不算头结点,直接返回
     3 //(2)注意头结点最后要单独处理问题
     4 #include <iostream>
     5 using namespace std;
     6 
     7 typedef struct linknode{
     8     int val;
     9     linknode * next;
    10 }node,*list;                  //加typedef说明node和list是类型,否则只是一个struct的变量
    11 
    12 void reverse(list head)
    13 {
    14     if(head==NULL||head->next==NULL||head->next->next==NULL)
    15         return;
    16     node * p=head->next;
    17     node * q=p->next;
    18     node * t;
    19     p->next=NULL;
    20     while(q!=NULL)
    21     {
    22         t=q->next;
    23         q->next=p;
    24         p=q;
    25         q=t;
    26     }
    27     head->next=p;
    28 }
    29 list createlist()
    30 {
    31     int data;
    32     list head=new node;
    33     head->val=0;
    34     head->next=NULL;
    35     node * p=head;
    36     while(cin>>data)
    37     {
    38         node * tmp=new node;
    39         tmp->val=data;
    40         tmp->next=NULL;
    41         p->next=tmp;
    42         p=tmp;
    43     }
    44     return head;
    45 }
    46 
    47 int main()
    48 {
    49     node * head=createlist();
    50     reverse(head);
    51     node *p=head->next;
    52     while(p)
    53     {
    54         node *q=p->next;
    55         cout<<p->val<<" ";
    56         delete p;
    57         p=q;
    58     }
    59     cout<<endl;
    60     system("pause");
    61 }
  • 相关阅读:
    basis 文档
    profile default1
    profile default
    2101244
    Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
    1816647
    lvm管理:扩展lv、删除pv、lv等
    HPUX and AIX SSH 互信
    SLD Related Gateway Serivces Unavaliable
    [原创]K8 MSF Bind Shell TCP 连接工具
  • 原文地址:https://www.cnblogs.com/zmlctt/p/3842566.html
Copyright © 2011-2022 走看看