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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

链表操作总结

發布時間:2025/3/21 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表操作总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

聲明

一年前閑著無聊時,研究研究了鏈表,當時寫的時候也花了不少時間,當時我的思路如下

動態鏈表帶頭結點

#include <iostream> using namespace std; class node { public:int id;node* next; };node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL; }head->id =-1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout <<"請輸入數據";cin>>data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout <<pCur->id<<"->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);cout << " ";system("pause");return 0; }

結點的插入

#include <iostream> using namespace std; class node { public:int id;node* next; }; //在值為x的結點前,插入值為y的結點,若x結點不存在,則插在表尾 int increasenode(node* head, int x, int y) {if (head == NULL){return -1;}node* Pre = head;node* Cur = head->next;while (Cur != NULL){if (Cur->id == x){break;}Pre = Cur;Cur = Cur->next;}node* pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {return -2;}pNew->id = y;pNew->next = NULL;Pre->next = pNew;pNew->next = Cur; }node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);int x; int y;cout << "分別輸入x和y,在x的前面插入y" << endl;cin >> x >> y;increasenode(head, x, y);cout << "在x的前面插入y:";ListPrint(head);cout << " ";system("pause");return 0;}

靜態鏈表

#include <iostream> using namespace std; class node { public:int id;char name[100];node* next;}; int main() {node s1 = { 1,"biaobiao",NULL };node s2 = { 2,"aha",NULL };node s3 = { 3,"leilei",NULL };s1.next = &s2;s2.next = &s3;s3.next = NULL;node* p = &s1;while (p != NULL) {cout << p->id << " " << p->name << " " << endl;p = p->next;} }

鏈表反轉

#include<iostream> using namespace std;class node { public:int id;node* next; }; int noderexerse(node* head) {if (head == NULL || head->next == NULL || head->next->next == NULL){return -1;}node* pre = head->next;node* cur = pre->next;node* temp=NULL;while (cur != NULL){temp=cur->next;cur->next = pre;pre = cur;cur = temp;}head->next->next = NULL;head->next = pre;return 0; }node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);noderexerse(head);cout << "反轉后為:" << endl;ListPrint(head); }

清空動態建立的鏈表

#include <iostream> using namespace std; class node { public:int id;node* next; }; //清空鏈表 int destroynode(node* head) {if (head == NULL) {return -1;}node* temp = NULL;int i = 0;while (head != NULL){temp = head->next;free(head);head = NULL;head = temp;i++;}cout << "釋放次數為:" << i<<endl;return 0; } node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);destroynode(head);ListPrint(head);cout << " ";system("pause");return 0; }

刪除第一個為x的結點

#include <iostream> using namespace std; class node { public:int id;node* next; }; int freecode(node*head,int x) {if (head == NULL){return -1;}node* Pre = head;node* Cur = head->next;int flag = 0;//0沒有找到,1找到了while (Cur != NULL){if (Cur->id == x){Pre->next = Cur->next;free(Cur);Cur = NULL;flag = 1;break;}Pre = Cur;Cur = Cur->next;}if (flag == 0){cout << "沒有值為" << x << "的結點" << endl;return -2;}} node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);int x;cout << "請輸入需要刪除的結點" << endl;cin >> x;freecode(head, x);cout << "刪除后結果為:" << endl;ListPrint(head);cout << " ";system("pause");return 0;}

刪除所有為x的結點

#include <iostream> using namespace std; class node { public:int id;node* next; }; int freecode(node* head, int x) {if (head == NULL){return -1;}node* Pre = head;node* Cur = head->next;int flag = 0;//0沒有找到,1找到了while (Cur != NULL){if (Cur->id == x){Pre->next = Cur->next;free(Cur);Cur = NULL;flag = 1;Cur = Pre->next;continue;//break;}Pre = Cur;Cur = Cur->next;}if (flag == 0){cout << "沒有值為" << x << "的結點" << endl;return -2;}} node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);int x;cout << "請輸入需要刪除的結點" << endl;cin >> x;freecode(head, x);cout << "刪除后結果為:" << endl;ListPrint(head);cout << " ";system("pause");return 0;}

升序鏈表插入

#include<iostream> using namespace std;struct node {int id;node* next; }; //假如原來鏈表是升序 的,升序后插入新節點 //不能插入結點后再排序,是升序插入新節點x int insertnode(node* head, int x) {if (head == NULL){return -1;}node* Pre = head;node* Cur = head->next;while (Cur != NULL){if (Cur->id > x){break;}Pre = Cur;Cur = Cur->next;}node* pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {return -2;}pNew->id = x;pNew->next = NULL;Pre->next = pNew;pNew->next = Cur;} int nodesort(node* head) {if (head == NULL || head->next == NULL){return 0;}node* pre = NULL;node* cur = NULL;node temp;for (pre = head->next; pre->next != NULL; pre = pre->next){for (cur = pre->next; cur != NULL; cur = cur->next){if (pre->id > cur->id){temp.id = cur->id;cur->id = pre->id;pre->id = temp.id;/*temp = *cur;*cur = *pre;*pre = temp;temp.next = cur->next;cur->next = pre->next;pre->next = temp.next;*/}}}return 0; }node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);nodesort(head);int x;cout << "輸入想要升序插入的數:" << endl;cin >> x;cout << "升序插入" << x << "后:" << endl;insertnode(head, x);ListPrint(head); }

用鏈表進行排序

#include<iostream> using namespace std; class node { public:int id;node* next; }; int nodesort(node* head) {if (head == NULL || head->next == NULL){return 0;}node* pre = NULL;node* cur = NULL;node temp;for (pre = head->next; pre->next != NULL; pre = pre->next){for (cur = pre->next; cur != NULL; cur = cur->next){if (pre->id > cur->id){temp.id = cur->id;cur->id = pre->id;pre->id = temp.id;/*temp = *cur;*cur = *pre;*pre = temp;temp.next = cur->next;cur->next = pre->next;pre->next = temp.next;*/}}}return 0; }node* ListCreat() {node* head = NULL;head = (node*)malloc(sizeof(node));if (head == NULL){return NULL;}head->id = -1;head->next = NULL;node* pCur = head;node* pNew = NULL;int data;while (1) {cout << "請輸入數據";cin >> data;if (data == -1) {break;}pNew = (node*)malloc(sizeof(node));if (pNew == NULL) {continue;}pNew->id = data;pNew->next = NULL;//鏈表建立關系pCur->next = pNew;pNew->next = NULL;pCur = pNew;}return head; } int ListPrint(node* head) {if (head == NULL){return -1;}//取出第一個有效結點,head的下一次結點node* pCur = head->next;cout << "head->";while (pCur != NULL){cout << pCur->id << "->";pCur = pCur->next;}cout << "NULL\n";return 0; } int main() {node* head = NULL;head = ListCreat();ListPrint(head);nodesort(head);cout << "排序后為:";ListPrint(head); }

總結

以上是生活随笔為你收集整理的链表操作总结的全部內容,希望文章能夠幫你解決所遇到的問題。

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