C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)

http://blog.csdn.net/xiaofeige567/article/details/27484137

C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)


[plain]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. typedef struct node  
  5. {  
  6.     int data;  
  7.     struct node *next;  
  8. }Linklist;  
  9.   
  10. Linklist *create() //创建链表,带头结点  
  11. {  
  12.     Linklist *head;  
  13.     head=(Linklist *)malloc(sizeof(Linklist));  
  14.     head->next=NULL;  
  15.     return head;      
  16. }  
  17.   
  18. Linklist *head_insert(Linklist *head,int value) //头插法,先插的元素排在后面  
  19. {  
  20.     Linklist *p,*t;  
  21.     t=head;  
  22.     p=(Linklist *)malloc(sizeof(Linklist));  
  23.     p->data=value;  
  24.     p->next=t->next;  
  25.     t->next=p;  
  26.     return head;  
  27. }  
  28.   
  29. Linklist *tail_insert(Linklist *head, int value) //尾插法  
  30. {  
  31.     Linklist *p,*t;  
  32.     t=head;  
  33.     p=(Linklist *)malloc(sizeof(Linklist));  
  34.     p->data=value;  
  35.     while(t->next!=NULL) //当链表不为空时t向后移动  
  36.     t=t->next;  
  37.   
  38.     t->next=p;  
  39.     p->next=NULL;  
  40.     return head;      
  41. }  
  42.   
  43. Linklist *reverse(Linklist *head) //链表逆置  
  44. {  
  45.     Linklist *p,*t;  
  46.     p=head->next;  
  47.     t=p->next;  
  48.     p->next=NULL;  
  49.     while(t!=NULL)  
  50.     {  
  51.       p=t->next;  
  52.       t->next=head->next;  
  53.       head->next=t;  
  54.       t=p;  
  55.     }  
  56.     return head;  
  57. }  
  58.   
  59. Linklist *display(Linklist *head) //打印链表数据  
  60. {  
  61.     Linklist *p;  
  62.     p=head->next;  
  63.     if(p==NULL)  
  64.     {  
  65.         printf("linklist is empty...\n");  
  66.         return ;  
  67.     }  
  68.     while(p!=NULL)  
  69.     {  
  70.       printf("%5d",p->data);  
  71.       p=p->next;   
  72.     }  
  73.     printf("\n");     
  74.     return head;  
  75. }  
  76.   
  77. Linklist  *delete(Linklist *head,int value) //删除结点  
  78. {  
  79.     Linklist *p,*t;  
  80.     p=head;  
  81.     while(p->next!=NULL)  
  82.     {  
  83.         if(p->next->data==value)  
  84.         {   
  85.             t=p->next;  
  86.             p->next=t->next;  
  87.             free(t);  
  88.             t=NULL;  
  89.         }  
  90.         else  
  91.             p=p->next;         
  92.     }  
  93.     return head;  
  94. }  
  95.   
  96. Linklist *sort(Linklist *head) //链表元素排序  
  97. {  
  98.     int i,j,t;  
  99.     int n=0;  
  100.     Linklist *p,*q;  
  101.     p=head->next;  
  102.      
  103.     while(p!=NULL)   
  104.     {  
  105.       n++;  
  106.       p=p->next;  
  107.       }  
  108.        
  109.        for(i=0;i<n-1;i++) //冒泡排序  
  110.       {  
  111.          p=head->next;  
  112.          q=p->next;  
  113.   
  114.             for(j=0;j<n-i-1;j++)  
  115.           {   
  116.               if(p->data > q->data)  
  117.              {  
  118.                t=p->data;  
  119.                p->data=q->data;  
  120.                q->data=t;  
  121.               }   
  122.               
  123.               p=p->next;  
  124.               q=q->next;     
  125.           }  
  126.   
  127.        }  
  128.     return head;  
  129. }  
  130.   
  131. int main()  
  132. {  
  133.     Linklist *head;  
  134.     int i,num;  
  135.     head=create();  
  136.   
  137.     printf("head_insert:\n");  
  138.     for(i=1;i<20;i=i+3)  
  139.     head_insert(head,i);  
  140.     display(head);  
  141.   
  142.     printf("linklist reverse:\n");  
  143.     reverse(head);  
  144.     display(head);  
  145.   
  146.     printf("tail_insert:\n");  
  147.     for(i=2;i<20;i=i+4)  
  148.     tail_insert(head,i);  
  149.     display(head);  
  150.       
  151.     printf("delete a node:");  
  152.     scanf("%d",&num);  
  153.     delete(head,num);  
  154.     display(head);  
  155.   
  156.     printf("linklist sort:\n");  
  157.     sort(head);  
  158.     display(head);  
  159.   
  160.     return 0;  
  161. }  

Linux下的运行结果:


  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值