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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)...

發布時間:2025/4/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#define?_CRT_SECURE_NO_WARNINGS?1 #include<iostream> using?namespace?std;//單鏈表的實現 #include<assert.h>typedef?int?DataType;typedef?struct?SListNode {DataType?_data;struct?SListNode*?_next; }SListNode;SListNode*?_CreateNode(DataType?x) {SListNode*?head?=?(SListNode*)malloc(sizeof(SListNode));head->_data?=?x;head->_next?=?NULL;return?head; }void?PushBack(SListNode*&?head,?DataType?x) {if?(head?==?NULL){head?=?_CreateNode(x);head->_next?=?NULL;}else{SListNode*?cur?=?head;while?(cur->_next?!=?NULL){cur?=?cur->_next;}cur->_next?=?_CreateNode(x);}}void?PopBack(SListNode*&?head) {if?(head?==?NULL){return;}?else?if?(head->_next?==?NULL){free(head);head?=?NULL;}else{SListNode*?cur?=?head;SListNode*?next?=?head;while?(cur){next?=?cur->_next;if?(next?!=?NULL?&&?next->_next?==?NULL){free(next);cur->_next?=?NULL;return;}cur?=?cur->_next;}}}void?PushFront(SListNode*&?head,DataType?x) {if?(head?==?NULL){head?=?_CreateNode(x);}else{SListNode*?pcur?=?_CreateNode(x);pcur->_next?=?head;head?=?pcur;} }void?PopFront(SListNode*&?head) {if?(head?==?NULL){return;}else?if?(head->_next?==?NULL){free(head);head?=?NULL;}else{SListNode*?del?=?head;SListNode*?next?=?head->_next;free(del);del?=?NULL;head?=?next;} }void?Insert(SListNode*?head,int?pos,DataType?x) {assert(pos?>=?0);SListNode*?cur?=?head;while?(--pos?&&?cur){????????????cur?=?cur->_next;????????}if?(pos?>?0){printf("pos位置大于鏈表長度!\n");return;}SListNode*?newcur?=?_CreateNode(x);if?(cur->_next){SListNode*?next?=?cur->_next;cur->_next?=?newcur;newcur->_next?=?next;}else?if?(cur->_next?==?NULL){cur->_next?=?newcur;} }size_t?Length(SListNode*&?head) {size_t?count?=?0;SListNode*?cur?=?head;while?(cur){count++;cur?=?cur->_next;}return?count; }void?PrintSList(SListNode*&?head) {if?(head?==?NULL){return;}SListNode*?cur?=?head;while?(cur){printf("%d->",?cur->_data);cur?=?cur->_next;}printf("\n"); }void?Test() {SListNode*?sList?=NULL;PushBack(sList,?1);PushBack(sList,?2);PushBack(sList,?3);PushBack(sList,?4);PushBack(sList,?5);PrintSList(sList);PopBack(sList);PrintSList(sList);PushFront(sList,?0);PrintSList(sList);PopFront(sList);PrintSList(sList);Insert(sList,?3,?10);PrintSList(sList);int?ret?=?Length(sList);printf("單鏈表長度為:%d\n",?ret); }int?main() {Test();system("pause");return?0; }



轉載于:https://blog.51cto.com/10740184/1747913

總結

以上是生活随笔為你收集整理的【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)...的全部內容,希望文章能夠幫你解決所遇到的問題。

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