C语言实现单链表的操作:创建,删除,插入,反转


刚学了数据结构的单链表基本操作:创建,删除,插入,反转等,以下是详细内容,其中很多关于数据结构的表述并不完整,只是简单的基本算法思想的表现。

由于没经验,代码有点乱·····

如有不当或错误之处,欢迎指正,不胜感激!

  1. #include<stdio.h>   
  2. #include <malloc.h>   
  3. struct node  
  4. {  
  5.        int data;  
  6.        struct node* next;                
  7. }head;    
  8. struct node *p,*q;//声明临时节点    
  9. void head_insert(int x)//从头部插入新节点    
  10. {  
  11.       
  12.      p=(struct node*)malloc(sizeof(struct node));  
  13.               if(p==NULL)  
  14.               {  
  15.                           printf("内存申请失败,退出");  
  16.                           exit(0);  
  17.               }   
  18.               p->data=x;  
  19.               p->next=head.next;  
  20.               head.next=p;      
  21. }     
  22. void tail_insert(int x)//从尾部插入节点    
  23. {  
  24.                 
  25.                p=(struct node*)malloc(sizeof(struct node));  
  26.                if(p==NULL)  
  27.               {  
  28.                           printf("内存申请失败,退出");  
  29.                           exit(0);  
  30.               }              
  31.               p->data=x;  
  32.               p->next=NULL;  
  33.               q->next=p;  
  34.               q=p;  
  35. }   
  36. void node_length()//输出链表长度    
  37. {  
  38.      int length=0;  
  39.      p=head.next;  
  40.       if(p==NULL) printf("The length of node is 0.\n");  
  41.     else   
  42.     {  
  43.     do  
  44.     {  
  45.             length++;  
  46.             p=p->next;                            
  47.     } while(p);  
  48.      printf("The length of node is %d.\n",length);  
  49.     }   
  50. }  
  51. void print_node()//打印链表    
  52. {  
  53.      printf("输出此时链表:\n");  
  54.       p=head.next;  
  55.       if(p==NULL) {printf("NULL\n");return;}  
  56.       else  
  57.       {  
  58.       while(p)  
  59.       {  
  60.       printf("%d ",p->data);  
  61.             p=p->next;           
  62.       }  
  63.       printf("\n");    
  64.       }                 
  65.       
  66. }    
  67. void clear_node()//清空链表    
  68. {  
  69.      p=head.next;  
  70.      head.next=NULL;  
  71.      while(p)  
  72.      {  
  73.              q=p;  
  74.              p=p->next;  
  75.              free(q);  
  76.      }  
  77. }  
  78. void new_insert(int i,int a)//在第i个位置后插入新整型元素 a   
  79. {  
  80.     q=(struct node*)malloc(sizeof(struct node));  
  81.     q->data=a;  
  82.     p=head.next;  
  83.     if(i<0) printf("Position Error.\n");  
  84.     else  
  85.    {   
  86.     while(p&&--i)  p=p->next;  
  87.     if(i)  printf("Position Error.\n");  
  88.     else  
  89.     {  
  90.     q->next=p->next;  
  91.     p->next=q;  
  92.     }  
  93.    }  
  94. }   
  95. void delete_node(int i)//删除某节点    
  96. {  
  97.      p=&head;  
  98.      while(p->next&&--i)              
  99.                p=p->next;  
  100.      if(i) printf("Position Error.\n");   
  101.      else  
  102.      {  
  103.          q=p->next;  
  104.          p->next=q->next;  
  105.         free(q);  
  106.      }  
  107. }   
  108. void invert_order()//将链表反转    
  109. {  
  110.      node *This,*prev;  
  111.      p=head.next;  
  112.      This=NULL;  
  113.      while(p)  
  114.      {  
  115.              prev=This;  
  116.              This=p;  
  117.              p=p->next;  
  118.              This->next=prev;  
  119.                
  120.      }  
  121.      head.next=This;  
  122. }  
  123. int main()  
  124. {  
  125.     int number,i,a;    
  126.     head.next=NULL;  
  127.     q=&head;  
  128.     printf("输入整型数据:\n");  
  129.     while(scanf("%d",&number)!=EOF)  //将数据存入链表    
  130.     {  
  131.             head_insert(number);//从头插入 即逆序插入    
  132.                 
  133.      /*     tail_insert(number); 从尾端 插入即正序插入       */    
  134.     }   
  135.     invert_order();  
  136.     node_length();//输出链表长度    
  137.     print_node();//输出链表   
  138.     printf("在第i个位置后插入a,请输入i和a:\n");  
  139.     scanf("%d%d",&i,&a);   
  140.     new_insert(i,a);  
  141.     print_node();  
  142.     printf("输入要删除的第i个结点:\n");  
  143.     scanf("%d",&i);  
  144.     delete_node(i);  
  145.     print_node();  
  146.     clear_node();  
  147.     return 0;  
  148. }  

附:在Windows下,输入数据完毕后先按Enter键,再按Ctrl+Z键,最后按Enter键即可结束输入;在Linux下,输入完毕后按Ctrl+D键可结束输入。

相关内容