鏈結串列:結點和指標
鏈結串列由一個個 malloc 出來的結點串成,每個結點存資料和指向下一個結點的 next 指標,散在堆上;用 p = p->next 一路走到 NULL,所以取第 i 個、求長度都是 O(n)。
陣列要一整塊連續記憶體,插入刪除要挪。鏈結串列換了個思路:每個元素單獨放,散在記憶體各處,每個元素自己記著「下一個在哪兒」。這一課用 C 搭一個單鏈表,把結點、指標和遍歷看清楚。
結點:資料加一個指標
鏈結串列的每一項叫一個結點(node):
1typedef struct Node {2 int data; // 這個結點存的數3 struct Node *next; // 下一個結點的位址;最後一個是 NULL4} Node;
結構體裡有一個指向同一種結構體的指標,所以要寫 struct Node *next(這時 Node 這個名字還沒定義好)。
鏈結串列最後一個結點的 next 裡放的是什麼?
把結點寫成 struct Node { int data; struct Node next; };(next 不是指標),行不行?
struct Node *next 沒問題。一個個 malloc 出來
每個結點用 malloc 在堆上要一塊。它們的位址各是各的,不連續;靠 next 串起來。
堆疊
headNode *堆
(圖裡每個框是一個結點;它的 next 存著下一個結點的位址,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 *c = new_node(9, NULL);18 Node *b = new_node(7, c);19 Node *a = new_node(3, b);20 Node *head = a;21 printf("%d %d %d\n", head->data, head->next->data, head->next->next->data);22 return 0;23}
3 7 9
點「單步」,看三塊記憶體依次出現在堆上,next 一根根連起來。head->next->next->data 就是順著指標走了兩步。
這幾個結點為了寫得短,直接放在堆疊上(真正建鏈結串列時用 malloc)。這段程式顯示什麼?
1#include <stdio.h>2 3typedef struct Node {4 int data;5 struct Node *next;6} Node;7 8int main(void) {9 Node c = {9, NULL}, b = {7, &c}, a = {3, &b};10 Node *head = &a;11 printf("%d\n", head->next->next->data - head->next->data);12 return 0;13}
head->next 是 7 那個結點,head->next->next 是 9 那個:9 − 7 = 2。每多一個 ->next,就順著指標多走一步。用 new_node(data, next) 建出 3 → 7 → 9。把這三行排好:建一個結點的時候,它的 next 要指的那個結點得已經有了。
Node *b = new_node(7, c);Node *a = new_node(3, b);Node *c = new_node(9, NULL);
3 7 9
遍歷:p = p->next 走到 NULL
走遍整個鏈結串列的寫法幾乎永遠是這三行:
p↓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(3, new_node(7, new_node(9, NULL)));18 for (Node *p = head; p != NULL; p = p->next)19 printf("%d -> ", p->data);20 printf("NULL\n");21 return 0;22}
3 -> 7 -> 9 -> NULL
迴圈條件寫成了 p->next != NULL,印出幾?
1#include <stdio.h>2 3typedef struct Node {4 int data;5 struct Node *next;6} Node;7 8int main(void) {9 Node c = {9, NULL}, b = {7, &c}, a = {3, &b};10 int n = 0;11 for (Node *p = &a; p->next != NULL; p = p->next)12 n++;13 printf("%d\n", n);14 return 0;15}
p->next 已經是 NULL,迴圈就停了,9 沒被數進去:只數到 2。p != NULL 是「走完每一個」,p->next != NULL 是「停在最後一個上」,兩種都常用,看你要哪種。選一個填進空格,讓 p 一個結點一個結點地往後走,算出 3 + 7 + 9。
1#include <stdio.h>2 3typedef struct Node {4 int data;5 struct Node *next;6} Node;7 8int main(void) {9 Node c = {9, NULL}, b = {7, &c}, a = {3, &b};10 int sum = 0;11 for (Node *p = &a; p != NULL; p = )12 sum += p->data;13 printf("%d\n", sum);14 return 0;15}
next 知道:p = p->next。鏈結串列的結點散在記憶體各處,不能像陣列那樣 p + 1。鏈結串列有多長
✓補全 length:從 head 走到 NULL,數一共幾個結點。
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 length(Node *head) {17 int n = 0;18 // p 從 head 走到 NULL19 return n;20}21 22int main(void) {23 Node *head = new_node(5, new_node(1, new_node(4, new_node(2, NULL))));24 printf("%d\n", length(head));25 return 0;26}
求長度、查詢都要走
鏈結串列沒有下標:要第 i 個,只能從 head 數過去。
| 操作 | 陣列 | 鏈結串列 |
|---|---|---|
| 取第 i 個 | O(1) | O(n):從頭走 i 步 |
| 求長度 | O(1)(記著 len) | O(n):走一遍數(除非也記著) |
| 查詢某個值 | O(n) | O(n) |
鏈結串列的長處在下一課:已經站在一個結點上時,在它後面插入、刪除只要 O(1)。
大 O 一樣的時候,實際跑起來陣列常常還更快:CPU 從記憶體讀資料時一次讀進一整段(快取行,通常 64 位元組)。陣列的下一個元素多半已經跟著讀進來了;鏈結串列的下一個結點在別處,往往要再去記憶體取一趟。
鏈結串列是 head → 5 → 1 → 4 → 2。p 從 head 出發(站在 5 上),要做幾次 p = p->next 才站到 4 上?
單鏈表只記著 head。下面哪些操作要 O(n)?(可以選好幾個)
找到它在第幾個
✓補全 find:返回第一個等於 x 的結點是第幾個(從 1 數起),找不到返回 0。
3 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 find(Node *head, int x) {17 int i = 1;18 // 一邊走一邊數,等於 x 就返回 i19 return 0;20}21 22int main(void) {23 Node *head = new_node(5, new_node(1, new_node(4, new_node(2, NULL))));24 printf("%d %d\n", find(head, 4), find(head, 7));25 return 0;26}
用完要 free
每個結點都是 malloc 來的,用完要一個個 free。順序很關鍵:先記住 next,再 free。反過來先 free(p) 再讀 p->next,讀的就是已經還回去的記憶體。
堆疊
headNode *nextNode *?堆
這樣釋放整條鏈結串列,行不行?for (Node *p = head; p != NULL; p = p->next) free(p);
p = p->next 在每一輪的 free(p) 之後才執行,讀的是已經還回去的那塊。在普通電腦上可能碰巧能跑,也可能崩;這裡的引擎會停下來告訴你。要先用一個變數記住 next,再 free。用完要還
✓補全 free_all:把每個結點都 free 掉,返回 free 了幾個。小心:free 之後就不能再讀它的 next 了。
3
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 free_all(Node *head) {17 int n = 0;18 // 先記住下一個,再 free 當前這個19 return n;20}21 22int main(void) {23 Node *head = new_node(5, new_node(1, new_node(4, NULL)));24 printf("%d\n", free_all(head));25 return 0;26}
小結
鏈結串列:結點散在堆上,靠 next 串起來
- 結點 = 資料 + 下一個結點的位址,最後一個是 NULL
- 遍歷:for (p = head; p != NULL; p = p->next)
- 沒有下標:取第 i 個、求長度都是 O(n)
- free 前先記住 next
1typedef struct Node { int data; struct Node *next; } Node;2for (Node *p = head; p != NULL; p = p->next)3 printf("%d ", p->data);