• 链表相关操作:创建链表、遍历链表、求链表长度、链表中删除一个节点、链表中插入一个节点、反转单链表


      1 #include<iostream>
      2 #include<stdlib.h>
      3 
      4 typedef struct node
      5 {
      6     int data;
      7     struct node *next;
      8 }Node,*pNode;
      9 
     10 pNode Creat_list();//创建一个单链表
     11 void Traverse_list(pNode head);//遍历链表
     12 int Len_list(pNode head);//求链表长度
     13 bool Delete_list(pNode head,int n,int &a);//删除链表的第n个节点,并将所删除节点的值传入a中
     14 bool Insert_list(pNode head,int n,int a);//在链表的第n个节点前插入一个新的节点,且新节点的值为n
     15 bool Reverse_list(pNode head);//反转单链表
     16 
     17 
     18 using namespace std;
     19 
     20 int main()
     21 {
     22     int delval;
     23     int length=0;
     24     pNode listhead=Creat_list();
     25     cout<<"链表元素为:";
     26     Traverse_list(listhead);
     27 
     28     cout<<endl;
     29     length=Len_list(listhead);
     30     cout<<"链表长度为:"<<length<<endl;
     31     cout<<endl;
     32 
     33     
     34     Delete_list(listhead,2,delval);
     35     cout<<"删除后链表元素为:";
     36     Traverse_list(listhead);
     37     cout<<"被删除的元素的值是:"<<delval;
     38     cout<<endl;
     39     
     40     
     41     Insert_list(listhead,1,2);
     42     cout<<"插入后链表元素为:";
     43     Traverse_list(listhead);
     44     cout<<endl;
     45 
     46     
     47     cout<<endl;
     48     Reverse_list(listhead);
     49     cout<<"反转后链表元素为:";
     50     Traverse_list(listhead);
     51     cout<<endl;
     52     
     53     cout<<endl;
     54     return 0;
     55 }
     56 
     57 
     58 
     59 //创建一个包含n个节点的单链表(不包括头结点)
     60 pNode Creat_list()
     61 {
     62     int n;//所创建链表元素的个数
     63     pNode head=(pNode)malloc(sizeof(Node));//创建链表的头结点
     64     head->next=NULL;
     65     cout<<"Please input the number of list element:";
     66     cin>>n;
     67     cout<<endl;
     68 
     69     if(n<=0)
     70     {
     71         return NULL;
     72     }
     73         
     74     else
     75     {
     76         pNode midnode=(pNode)malloc(sizeof(Node));
     77         midnode=head;
     78 
     79         for(int i=1;i<n+1;i++)
     80         {
     81             int a;
     82             cout<<"Please input the number"<<i<<"'s data:";
     83             cin>>a;
     84 
     85             pNode newnode=(pNode)malloc(sizeof(Node));
     86             if(newnode==NULL)
     87             {
     88                 cout<<"创建新节点失败"<<endl;
     89                 return NULL;
     90             }
     91 
     92             else
     93             {
     94                 midnode->next=newnode;//现在的midnode指向的是newnode的上一个节点
     95                 newnode->data=a;
     96                 newnode->next=NULL;
     97                 midnode=newnode;//现在的midnode指向最新的节点
     98             }
     99         }
    100         
    101         return head;
    102     }
    103 }
    104 
    105 
    106 
    107 //遍历链表
    108 void Traverse_list(pNode head)
    109 {
    110     if(head->next == NULL)
    111     {
    112         cout<<"链表为空"<<endl;
    113         return;
    114     }
    115 
    116     else
    117     {
    118         pNode midnode=(pNode)malloc(sizeof(Node));
    119         midnode=head;
    120 
    121         while(midnode->next != NULL)
    122         {
    123             midnode=midnode->next;
    124             cout<<midnode->data<<" ";
    125         }
    126         cout<<endl;
    127 
    128         return;
    129     }
    130 }
    131 
    132 
    133 //求链表长度
    134 int Len_list(pNode head)
    135 {
    136     int len=0;
    137 
    138     if(head->next==NULL)
    139     {
    140         return 0;
    141     }
    142 
    143     else
    144     {
    145         pNode midnode=(pNode)malloc(sizeof(Node));
    146 
    147         if(midnode==NULL)
    148         {
    149             cout<<"分配中间节点内存失败!";
    150             return 0;
    151         }
    152 
    153         midnode=head;
    154         while(midnode->next != NULL)
    155         {
    156             midnode=midnode->next;
    157             len++;
    158         }
    159         return len;
    160     }
    161 }
    162 
    163 
    164 
    165 //删除链表中的第n个节点(头结点视为链表的第0个节点),并返回所删除元素的值到a中
    166 bool Delete_list(pNode head,int n,int &a)
    167 {
    168     int length=Len_list(head);//链表的长度
    169 
    170     if(n<=0 || n>length)
    171     {
    172         cout<<"被删除元素的序数非法!"<<endl;
    173         return false;
    174     }
    175 
    176     else if(n==1)
    177     {
    178         pNode midnode=(pNode)malloc(sizeof(Node));
    179         midnode=head->next;
    180         a=midnode->data;
    181         head->next=midnode->next;
    182         free(midnode);
    183 
    184         return true;
    185     }
    186 
    187 
    188     else
    189     {
    190         int i=0;
    191 
    192         pNode midnode1=(pNode)malloc(sizeof(Node));
    193         midnode1=head;
    194 
    195         while(i<n-1)
    196         {
    197             midnode1=midnode1->next;
    198             i++;
    199         }//出while循环时,midnode指向的是第n-1个节点
    200 
    201         pNode midnode2=(pNode)malloc(sizeof(Node));
    202         midnode2=midnode1->next;//midnode2是被删除的节点
    203         a=midnode2->data;
    204         midnode1->next=midnode2->next;
    205         free(midnode2);
    206 
    207         return true;
    208     }
    209 }
    210 
    211 
    212 //在链表中第n个节点前插入一个值为a的新节点
    213 bool Insert_list(pNode head,int n,int a)
    214 {
    215     int len=Len_list(head);
    216 
    217     if(n<=0 || n>len)
    218     {
    219         cout<<"插入元素的位置非法!";
    220         return false;
    221     }
    222 
    223     else if(n==1)
    224     {
    225         pNode midnode=(pNode)malloc(sizeof(Node));
    226         if(midnode==NULL)
    227         {
    228             cout<<"分配中间节点内存失败!";
    229             return false;
    230         }
    231 
    232         midnode->data=a;
    233         midnode->next=head->next;
    234         head->next=midnode;
    235 
    236         return true;
    237     }
    238 
    239     else
    240     {
    241         int i=0;
    242 
    243         pNode midnode1=(pNode)malloc(sizeof(Node));
    244         if(midnode1==NULL)
    245         {
    246             cout<<"分配中间节点内存失败!";
    247             return false;
    248         }
    249 
    250         midnode1=head;
    251         while(i<n-1)
    252         {
    253             midnode1=midnode1->next;
    254             i++;
    255         }//出while循环时midnode1指向的是第n-1个节点
    256         
    257 
    258         pNode midnode2=(pNode)malloc(sizeof(Node));
    259         if(midnode2==NULL)
    260         {
    261             cout<<"分配新节点内存失败!";
    262             return false;
    263         }
    264     
    265         midnode2->data=a;
    266         midnode2->next=midnode1->next;
    267         midnode1->next=midnode2;
    268 
    269         return true;
    270     }
    271 }
    272 
    273 
    274 //反转单链表
    275 bool Reverse_list(pNode head)
    276 {
    277     int len=Len_list(head);
    278 
    279     if(len==0 || len==1)
    280     {
    281         cout<<"链表元素过少,不能反转!";
    282         return false;
    283     }
    284 
    285     else
    286     {
    287         pNode midnode1=(pNode)malloc(sizeof(Node));
    288         pNode midnode2=(pNode)malloc(sizeof(Node));
    289         if(midnode1==NULL || midnode2==NULL)
    290         {
    291             cout<<"分配中间节点内存失败!";
    292             return false;
    293         }
    294 
    295         midnode1=head->next;
    296         midnode2=midnode1->next;
    297         midnode1->next=NULL;
    298 
    299         while(midnode2->next != NULL)
    300         {
    301             pNode X=midnode2->next;
    302             midnode2->next=midnode1;
    303             midnode1=midnode2;
    304             midnode2=X;
    305         }
    306         midnode2->next=midnode1;
    307         head->next=midnode2;
    308 
    309         return true;
    310     }
    311 }
  • 相关阅读:
    深圳的2022
    Erlang port communicate with python
    Google v8实现类似nodejs的import加载模块
    手撕某解析网站,获取真实播放地址(保姆级讲解)(转)
    存储过程中组建集合
    svn cleanup 系统找不到指定路径_SVN:cleanup failed to process the following paths 错误
    安装Android Studio时,没有sdk列表
    html 打印时 &nbsp; &nbsp; 空格不等于一个汉字问题
    使用layui出现Uncaught ReferenceError: layui is not defined
    OKR基础知识(三)
  • 原文地址:https://www.cnblogs.com/jswu-ustc/p/8315676.html
Copyright © 2020-2023  润新知