題型與套路:順序表和鏈結串列
這一章的順序表和鏈結串列總複習講五類題:順序表和鏈結串列怎麼選、頭插法和尾插法建表、用三個指標原地逆置、快慢指標找中間和判環、合併兩個有序鏈結串列。
學完順序表和鏈結串列,期末、考研 408 和麵試最常考的就是這幾類題,這一課一關講一類。每一類記住一個套路,碰到變形也能套上去。
選擇題:順序表還是鏈結串列
先問自己:這份資料最常做的操作是什麼?
| 最常做的 | 選 | 為什麼 |
|---|---|---|
| 按位置取(第 i 個) | 順序表 | O(1);鏈結串列要走 i 步 |
| 在已知結點前後插入、刪除 | 鏈結串列 | O(1);順序表要挪 |
| 事先不知道有多少個 | 鏈結串列(或動態陣列) | 不用一次要一大塊 |
| 很在意記憶體、要快取友好 | 順序表 | 不用存指標,連續記憶體讀得快 |
常見陷阱: 「鏈結串列插入是 O(1)」前提是已經拿到前一個結點;「在第 i 個位置插入」兩種都要 O(n):順序錶慢在挪,鏈結串列慢在找。
要存全班的成績,最常做的是「查第 i 個同學的成績」,幾乎不插入、刪除。選哪個?
a[i] 直接算出位址,O(1);鏈結串列要從頭走 i 步,O(n)。幾乎不插刪,鏈結串列的長處也用不上。頭插法和尾插法:建表
依次讀進 1 2 3 4 5 建一個帶頭結點的鏈結串列,有兩種插法:
- 頭插法:每個新結點都插在頭結點後面。建出來是反的:5 4 3 2 1。
- 尾插法:用一個
tail指標記著最後一個,每次接在它後面。順序不變。
L↓L↓tail↓1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16void show(Node *L) {17 for (Node *p = L->next; p != NULL; p = p->next)18 printf("%d ", p->data);19 printf("\n");20}21 22int main(void) {23 // 頭插法:插在頭結點後面24 Node *A = new_node(0, NULL);25 for (int x = 1; x <= 5; x++)26 A->next = new_node(x, A->next);27 show(A);28 29 // 尾插法:接在 tail 後面30 Node *B = new_node(0, NULL);31 Node *tail = B;32 for (int x = 1; x <= 5; x++) {33 tail->next = new_node(x, NULL);34 tail = tail->next;35 }36 show(B);37 return 0;38}
5 4 3 2 1 1 2 3 4 5
所以「把一個鏈結串列逆過來」的一種辦法就是:把結點一個個拆下來,用頭插法插回去。
尾插法忘了挪 tail,印出什麼?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *L = new_node(0, NULL);18 Node *tail = L;19 for (int x = 1; x <= 3; x++)20 tail->next = new_node(x, NULL);21 for (Node *p = L->next; p != NULL; p = p->next)22 printf("%d ", p->data);23 printf("\n");24 return 0;25}
tail 一直指著頭結點,每次都是 L->next = 新結點,新來的把前一個頂掉了:最後只剩 3(1 和 2 還洩漏了)。尾插法每接一個,都要 tail = tail->next;。原地逆置
不另外要記憶體,把 1 → 2 → 3 改成 3 → 2 → 1。套路是三個指標:prev(已經反好的那段的頭)、cur(正在處理的)、next(先記住下一個,免得斷了找不到)。
cur↓1Node *prev = NULL, *cur = head;2while (cur != NULL) {3 // 1. 先記住下一個4 Node *next = cur->next;5 // 2. 掉頭6 cur->next = prev;7 // 3. 兩個指標都往前挪8 prev = cur;9 cur = next;10}11// 新的頭12head = prev;
每個結點只碰一次:O(n) 時間,O(1) 空間。
把迴圈裡的這 4 行排好,讓程式把 1 2 3 4 逆置成 4 3 2 1。
prev = cur;cur = next;cur->next = prev;Node *next = cur->next;
4 3 2 1
prev = cur; 要是放在 cur = next; 後面,prev 就跟錯了結點。原地逆置
✓補全 reverse:用三個指標把鏈結串列原地逆過來,返回新的頭。
4 3 2 1
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16void show(Node *head) {17 for (Node *p = head; p != NULL; p = p->next)18 printf("%d ", p->data);19 printf("\n");20}21 22Node *reverse(Node *head) {23 Node *prev = NULL, *cur = head;24 // 記住下一個、掉頭、兩個都往前挪25 return prev;26}27 28int main(void) {29 Node *head = new_node(1, new_node(2, new_node(3, new_node(4, NULL))));30 head = reverse(head);31 show(head);32 return 0;33}
快慢指標:找中間、判有環
兩個指標同時從頭出發,slow 每次走 1 步,fast 每次走 2 步。fast 走到頭時,slow 正好在中間。
slow↓fast↓1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *head = new_node(1, new_node(2, new_node(3, new_node(4, new_node(5, NULL)))));18 Node *slow = head, *fast = head;19 while (fast != NULL && fast->next != NULL) {20 slow = slow->next;21 fast = fast->next->next;22 }23 printf("中間是 %d\n", slow->data);24 return 0;25}
中間是 3
同一個套路還能判斷有沒有環:要是鏈結串列某處指回了前面,fast 永遠走不到 NULL,而且總會從後面追上 slow(兩個指標相等)。
slow↓fast↓用快慢指標判斷有沒有環,時間和額外空間各是多少?
有沒有環
✓補全 has_cycle:用快慢指標判斷鏈結串列有沒有環,有返回 1,沒有返回 0。
1 0
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int has_cycle(Node *head) {17 Node *slow = head, *fast = head;18 // fast 能走就走兩步,slow 走一步;相等就是有環19 return 0;20}21 22int main(void) {23 Node *c = new_node(3, NULL);24 Node *a = new_node(1, new_node(2, c));25 Node *x = new_node(7, new_node(8, new_node(9, NULL)));26 c->next = a->next; // 3 指回 2:有環27 printf("%d %d\n", has_cycle(a), has_cycle(x));28 return 0;29}
合併兩個有序鏈結串列
兩個從小到大排好的鏈結串列,合成一個仍然有序的。套路是一個假的頭結點 + 一個 tail:每次比較兩邊的第一個,小的接到 tail 後面。
1Node dummy = {0, NULL};2Node *tail = &dummy;3while (a != NULL && b != NULL) {4 if (a->data <= b->data) { tail->next = a; a = a->next; }5 else { tail->next = b; b = b->next; }6 tail = tail->next;7}8// 剩下的整段接上9tail->next = (a != NULL) ? a : b;10return dummy.next;
用了假的頭結點,第一個結點就不用特殊處理(和「鏈結串列的插入和刪除」那一課裡的頭結點是同一個道理)。
迴圈停下時,有一邊已經走完了。最後一行要把另一邊剩下的整段接上,空裡填哪個?程式要印出 1 2 3 4 6 8 9。
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16Node *merge(Node *a, Node *b) {17 Node dummy = {0, NULL};18 Node *tail = &dummy;19 while (a != NULL && b != NULL) {20 if (a->data <= b->data) {21 tail->next = a;22 a = a->next;23 } else {24 tail->next = b;25 b = b->next;26 }27 tail = tail->next;28 }29 tail->next = ;30 return dummy.next;31}32 33int main(void) {34 Node *a = new_node(1, new_node(4, new_node(8, new_node(9, NULL))));35 Node *b = new_node(2, new_node(3, new_node(6, NULL)));36 for (Node *p = merge(a, b); p != NULL; p = p->next)37 printf("%d ", p->data);38 printf("\n");39 return 0;40}
b 或 NULL 都會把 8 9 丟掉。合併兩個有序鏈結串列
✓補全 merge:用假的頭結點和 tail,把兩個從小到大的鏈結串列合成一個。
1 2 3 4 6 8 9
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16void show(Node *head) {17 for (Node *p = head; p != NULL; p = p->next)18 printf("%d ", p->data);19 printf("\n");20}21 22Node *merge(Node *a, Node *b) {23 Node dummy = {0, NULL};24 Node *tail = &dummy;25 // 兩邊都還有:小的接到 tail 後面;最後把剩下的整段接上26 return dummy.next;27}28 29int main(void) {30 Node *a = new_node(1, new_node(4, new_node(6, NULL)));31 Node *b = new_node(2, new_node(3, new_node(8, new_node(9, NULL))));32 show(merge(a, b));33 return 0;34}
小結
鏈結串列五個套路
- 選擇:按位置取用順序表,頻繁插刪用鏈結串列
- 頭插法建出來是反的,尾插法要一個 tail
- 逆置:prev、cur、next 三個指標
- 快慢指標:找中間、判有環
- 合併有序:假的頭結點 + tail
1next = cur->next; cur->next = prev; prev = cur; cur = next;2slow = slow->next; fast = fast->next->next;
第 1 章快刷
6 道題,覆蓋這一章的順序表、鏈結串列、動態陣列和最容易寫錯的那一步。先自己算出印出什麼,再點選項。
長度 6 的順序表在下標 2 插入,挪了幾個?
1#include <stdio.h>2 3int main(void) {4 int a[8] = {1, 2, 3, 4, 5, 6};5 int len = 6, pos = 2, moves = 0;6 for (int i = len; i > pos; i--) {7 a[i] = a[i - 1];8 moves++;9 }10 a[pos] = 9;11 printf("%d\n", moves);12 return 0;13}
p 是第二個結點,執行 p->next = p->next->next 以後印出什麼?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *head = new_node(1, new_node(2, new_node(3, new_node(4, NULL))));18 Node *p = head->next;19 p->next = p->next->next;20 for (Node *q = head; q != NULL; q = q->next)21 printf("%d ", q->data);22 printf("\n");23 return 0;24}
每次 head = new_node(x, head),印出什麼?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *head = NULL;18 for (int x = 1; x <= 4; x++)19 head = new_node(x, head);20 for (Node *q = head; q != NULL; q = q->next)21 printf("%d ", q->data);22 printf("\n");23 return 0;24}
1 到 6 共 6 個結點,快慢指標停下時 slow 指著幾?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *head = NULL;18 for (int x = 6; x >= 1; x--)19 head = new_node(x, head);20 Node *slow = head, *fast = head;21 while (fast != NULL && fast->next != NULL) {22 slow = slow->next;23 fast = fast->next->next;24 }25 printf("%d\n", slow->data);26 return 0;27}
從 1 格開始,滿了就翻倍,放進 9 個以後 len 和 cap 是多少?
1#include <stdio.h>2 3int main(void) {4 int cap = 1, len = 0;5 for (int x = 0; x < 9; x++) {6 if (len == cap)7 cap *= 2;8 len++;9 }10 printf("%d %d\n", len, cap);11 return 0;12}
插入時兩步寫反了:先改 p->next,再接 s->next。印出前 4 個是什麼?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *p = new_node(1, new_node(2, NULL));18 Node *s = new_node(5, NULL);19 p->next = s;20 s->next = p->next;21 Node *q = p;22 for (int k = 0; k < 4; k++, q = q->next)23 printf("%d ", q->data);24 printf("\n");25 return 0;26}