深入链表,将链表进行升序排列
|字数总计:3.4k|阅读时长:15分钟|阅读量:|
历经数天磨练(Bug快把我侵蚀),我终于将这款将链表合并然后进行升序排列程序做出来啦~撒花✿✿ヽ(°▽°)ノ✿
一.代码组成
本代码是两个程序
- 第一个程序是先将两个单链表合并,然后再对链表进行插入排序
- 第二个程序是先将两个单链表进行排序,然后再将两个有序单链表进行合并
两个程序演示如下(仔细看,有区别):
二.代码部分
话不多说,上代码:
第一个程序:单链表排序合并.cpp
第一部分
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std;
struct node { int num; struct node* next; };
|
第二部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| node* create(void) { struct node* head = nullptr, * tail = nullptr, * tmp = nullptr; node* list[10000] = { nullptr }; int int_num = 1, i = 0; cout << "请输入链表,以0结尾:" << endl; cin >> int_num; while (int_num != 0) { list[i] = new node({ int_num, nullptr }); cin >> int_num; i++; } for (int j = 0; j <= i - 2; j++) { list[j]->next = list[j + 1]; } return list[0]; }
|
第三部分
1 2 3 4 5 6 7 8 9 10 11
| void output(node* list0) { if (list0 == nullptr) { cout << "链表为空"; } while (list0 != nullptr) { cout << list0->num << " "; list0 = list0->next; } return; }
|
第四部分
1 2 3 4 5 6 7 8 9 10 11 12
| node* merge(node* list1, node* list2) { if (list1 == nullptr) { return list2; } node* list_sum = list1; while (list1->next != nullptr) { list1 = list1->next; } list1->next = list2; return list_sum; }
|
第五部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| node* sort_myself(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr, * curr = nullptr; curr = list12;
while (curr != nullptr) { node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
|
第六部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| node* sort_chatgpt(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr; struct node* curr = list12;
while (curr != nullptr) { struct node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
|
第七部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| int main() { cout << "声明:\n链表数据个数上限为10000\n还有,数不要输太大,不然会整数溢出" << endl; node* list1 = create(); node* list2 = create(); cout << "第一个链表为:" << endl; output(list1); cout << endl << "第二个链表为:" << endl; output(list2); cout << endl << "将两链表合并:" << endl; node* list12 = merge(list1, list2); output(list12); cout << endl << "将链表以升序排列:" << endl << "我写的:" << endl; node* list_all_myself = sort_myself(list12); output(list_all_myself); cout << endl << "ChatGPT写的:" << endl; node* list_all_gpt = sort_chatgpt(list12); output(list_all_gpt); return 0; }
|
全部代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <iostream> using namespace std;
struct node { int num; struct node* next; };
node* create(void) { struct node* head = nullptr, * tail = nullptr, * tmp = nullptr; node* list[10000] = { nullptr }; int int_num = 1, i = 0; cout << "请输入链表,以0结尾:" << endl; cin >> int_num; while (int_num != 0) { list[i] = new node({ int_num, nullptr }); cin >> int_num; i++; } for (int j = 0; j <= i - 2; j++) { list[j]->next = list[j + 1]; } return list[0]; }
void output(node* list0) { if (list0 == nullptr) { cout << "链表为空"; } while (list0 != nullptr) { cout << list0->num << " "; list0 = list0->next; } return; }
node* merge(node* list1, node* list2) { if (list1 == nullptr) { return list2; } node* list_sum = list1; while (list1->next != nullptr) { list1 = list1->next; } list1->next = list2; return list_sum; }
node* sort_myself(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr, * curr = nullptr; curr = list12;
while (curr != nullptr) { node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
node* sort_chatgpt(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr; struct node* curr = list12;
while (curr != nullptr) { struct node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
int main() { cout << "声明:\n链表数据个数上限为10000\n还有,数不要输太大,不然会整数溢出" << endl; node* list1 = create(); node* list2 = create(); cout << "第一个链表为:" << endl; output(list1); cout << endl << "第二个链表为:" << endl; output(list2); cout << endl << "将两链表合并:" << endl; node* list12 = merge(list1, list2); output(list12); cout << endl << "将链表以升序排列:" << endl << "我写的:" << endl; node* list_all_myself = sort_myself(list12); output(list_all_myself); cout << endl << "ChatGPT写的:" << endl; node* list_all_gpt = sort_chatgpt(list12); output(list_all_gpt); return 0; }
|
第二个程序:两个有序链表排序合并.cpp
第一部分
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std;
struct node { int num; struct node* next; };
|
第二部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| node* create(void) { struct node* head = nullptr, * tail = nullptr, * tmp = nullptr; node* list[10000] = { nullptr }; int int_num = 1, i = 0; cout << "请输入链表,以0结尾:" << endl; cin >> int_num; while (int_num != 0) { list[i] = new node({ int_num, nullptr }); cin >> int_num; i++; } for (int j = 0; j <= i - 2; j++) { list[j]->next = list[j + 1]; } return list[0]; }
|
第三部分
1 2 3 4 5 6 7 8 9 10 11
| void output(node* list0) { if (list0 == nullptr) { cout << "链表为空"; } while (list0 != nullptr) { cout << list0->num << " "; list0 = list0->next; } return; }
|
第四部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| node* sort(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr, * curr = nullptr; curr = list12;
while (curr != nullptr) { struct node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
|
第五部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| node* merge(node* list1, node* list2) { struct node* head = nullptr, * tail = nullptr;
if (list1->num <= list2->num) { head = list1; } else { head = list2; }
tail = head;
list1 = list1->next; list2 = list2->next;
while (list1 && list2) { if (list1->num <= list2->num) { tail->next = list1; tail = list1; list1 = list1->next; } else { tail->next = list2; tail = list2; list2 = list2->next; } }
while (list1) { tail->next = list1; tail = list1; list1 = list1->next; } while (list2) { tail->next = list2; tail = list2; list2 = list2->next; }
return head; }
|
第六部分
1 2 3 4 5 6 7 8 9 10 11 12
| int main() { cout << "声明:\n链表数据个数上限为10000\n还有,数不要输太大,不然会整数溢出" << endl; node* list1 = sort(create()); node* list2 = sort(create()); cout << "第一个链表为:" << endl; output(list1); cout << endl << "第二个链表为:" << endl; output(list2); cout << endl << "将两个链表合并,然后按升序排列:" << endl; output(merge(list1, list2)); return 0; }
|
全部代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <iostream> using namespace std;
struct node { int num; struct node* next; };
node* create(void) { struct node* head = nullptr, * tail = nullptr, * tmp = nullptr; node* list[10000] = { nullptr }; int int_num = 1, i = 0; cout << "请输入链表,以0结尾:" << endl; cin >> int_num; while (int_num != 0) { list[i] = new node({ int_num, nullptr }); cin >> int_num; i++; } for (int j = 0; j <= i - 2; j++) { list[j]->next = list[j + 1]; } return list[0]; }
void output(node* list0) { if (list0 == nullptr) { cout << "链表为空"; } while (list0 != nullptr) { cout << list0->num << " "; list0 = list0->next; } return; }
node* sort(node* list12) { if (list12 == nullptr || list12->next == nullptr) { return list12; }
struct node* head = nullptr, * curr = nullptr; curr = list12;
while (curr != nullptr) { struct node* next = curr->next; if (head == nullptr || curr->num < head->num) { curr->next = head; head = curr; } else { struct node* prev = head; while (prev->next != nullptr && curr->num > prev->next->num) { prev = prev->next; } curr->next = prev->next; prev->next = curr; } curr = next; } return head; }
node* merge(node* list1, node* list2) { struct node* head = nullptr, * tail = nullptr;
if (list1->num <= list2->num) { head = list1; } else { head = list2; }
tail = head;
list1 = list1->next; list2 = list2->next;
while (list1 && list2) { if (list1->num <= list2->num) { tail->next = list1; tail = list1; list1 = list1->next; } else { tail->next = list2; tail = list2; list2 = list2->next; } }
while (list1) { tail->next = list1; tail = list1; list1 = list1->next; } while (list2) { tail->next = list2; tail = list2; list2 = list2->next; }
return head; }
int main() { cout << "声明:\n链表数据个数上限为10000\n还有,数不要输太大,不然会整数溢出" << endl; node* list1 = sort(create()); node* list2 = sort(create()); cout << "第一个链表为:" << endl; output(list1); cout << endl << "第二个链表为:" << endl; output(list2); cout << endl << "将两个链表合并,然后按升序排列:" << endl; output(merge(list1, list2)); return 0; }
|
写这两段代码可不容易(头发没了😭😭😭