日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/11/30 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

C語言實現單鏈表(帶頭結點)的基本操作(創建,頭插法,尾插法,刪除結點,打印鏈表)


[plain]?view plain?copy
  • #include<stdio.h>??
  • #include<stdlib.h>??
  • ??
  • typedef?struct?node??
  • {??
  • ????int?data;??
  • ????struct?node?*next;??
  • }Linklist;??
  • ??
  • Linklist?*create()?//創建鏈表,帶頭結點??
  • {??
  • ????Linklist?*head;??
  • ????head=(Linklist?*)malloc(sizeof(Linklist));??
  • ????head->next=NULL;??
  • ????return?head;??????
  • }??
  • ??
  • Linklist?*head_insert(Linklist?*head,int?value)?//頭插法,先插的元素排在后面??
  • {??
  • ????Linklist?*p,*t;??
  • ????t=head;??
  • ????p=(Linklist?*)malloc(sizeof(Linklist));??
  • ????p->data=value;??
  • ????p->next=t->next;??
  • ????t->next=p;??
  • ????return?head;??
  • }??
  • ??
  • Linklist?*tail_insert(Linklist?*head,?int?value)?//尾插法??
  • {??
  • ????Linklist?*p,*t;??
  • ????t=head;??
  • ????p=(Linklist?*)malloc(sizeof(Linklist));??
  • ????p->data=value;??
  • ????while(t->next!=NULL)?//當鏈表不為空時t向后移動??
  • ????t=t->next;??
  • ??
  • ????t->next=p;??
  • ????p->next=NULL;??
  • ????return?head;??????
  • }??
  • ??
  • Linklist?*reverse(Linklist?*head)?//鏈表逆置??
  • {??
  • ????Linklist?*p,*t;??
  • ????p=head->next;??
  • ????t=p->next;??
  • ????p->next=NULL;??
  • ????while(t!=NULL)??
  • ????{??
  • ??????p=t->next;??
  • ??????t->next=head->next;??
  • ??????head->next=t;??
  • ??????t=p;??
  • ????}??
  • ????return?head;??
  • }??
  • ??
  • Linklist?*display(Linklist?*head)?//打印鏈表數據??
  • {??
  • ????Linklist?*p;??
  • ????p=head->next;??
  • ????if(p==NULL)??
  • ????{??
  • ????????printf("linklist?is?empty...\n");??
  • ????????return?;??
  • ????}??
  • ????while(p!=NULL)??
  • ????{??
  • ??????printf("%5d",p->data);??
  • ??????p=p->next;???
  • ????}??
  • ????printf("\n");?????
  • ????return?head;??
  • }??
  • ??
  • Linklist??*delete(Linklist?*head,int?value)?//刪除結點??
  • {??
  • ????Linklist?*p,*t;??
  • ????p=head;??
  • ????while(p->next!=NULL)??
  • ????{??
  • ????????if(p->next->data==value)??
  • ????????{???
  • ????????????t=p->next;??
  • ????????????p->next=t->next;??
  • ????????????free(t);??
  • ????????????t=NULL;??
  • ????????}??
  • ????????else??
  • ????????????p=p->next;?????????
  • ????}??
  • ????return?head;??
  • }??
  • ??
  • Linklist?*sort(Linklist?*head)?//鏈表元素排序??
  • {??
  • ????int?i,j,t;??
  • ????int?n=0;??
  • ????Linklist?*p,*q;??
  • ????p=head->next;??
  • ?????
  • ????while(p!=NULL)???
  • ????{??
  • ??????n++;??
  • ??????p=p->next;??
  • ??????}??
  • ???????
  • ???????for(i=0;i<n-1;i++)?//冒泡排序??
  • ??????{??
  • ?????????p=head->next;??
  • ?????????q=p->next;??
  • ??
  • ????????????for(j=0;j<n-i-1;j++)??
  • ??????????{???
  • ??????????????if(p->data?>?q->data)??
  • ?????????????{??
  • ???????????????t=p->data;??
  • ???????????????p->data=q->data;??
  • ???????????????q->data=t;??
  • ??????????????}???
  • ??????????????
  • ??????????????p=p->next;??
  • ??????????????q=q->next;?????
  • ??????????}??
  • ??
  • ???????}??
  • ????return?head;??
  • }??
  • ??
  • int?main()??
  • {??
  • ????Linklist?*head;??
  • ????int?i,num;??
  • ????head=create();??
  • ??
  • ????printf("head_insert:\n");??
  • ????for(i=1;i<20;i=i+3)??
  • ????head_insert(head,i);??
  • ????display(head);??
  • ??
  • ????printf("linklist?reverse:\n");??
  • ????reverse(head);??
  • ????display(head);??
  • ??
  • ????printf("tail_insert:\n");??
  • ????for(i=2;i<20;i=i+4)??
  • ????tail_insert(head,i);??
  • ????display(head);??
  • ??????
  • ????printf("delete?a?node:");??
  • ????scanf("%d",&num);??
  • ????delete(head,num);??
  • ????display(head);??
  • ??
  • ????printf("linklist?sort:\n");??
  • ????sort(head);??
  • ????display(head);??
  • ??
  • ????return?0;??
  • }??

  • Linux下的運行結果:


    總結

    以上是生活随笔為你收集整理的C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。