Kodewick
開始學 →
鏈結串列:結點和指標7 / 50

鏈結串列:結點和指標

第 7 課,共 50 課 · 第 2 部分 · 順序表和鏈結串列 · 約 14 分鐘

鏈結串列由一個個 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 這個名字還沒定義好)。

3a
NULL
一個結點:data 裡是 3,next 裡是 NULL(後面沒有了)
單選

鏈結串列最後一個結點的 next 裡放的是什麼?

單選

把結點寫成 struct Node { int data; struct Node next; };(next 不是指標),行不行?

一個個 malloc 出來

每個結點用 malloc 在堆上要一塊。它們的位址各是各的,不連續;靠 next 串起來。

堆疊

headNode *

堆

0x5a403Node
0x5a107Node
0x5a909Node
三個結點散在堆上,靠 next 裡的位址串成 3 → 7 → 9

(圖裡每個框是一個結點;它的 next 存著下一個結點的位址,head 存著第一個的位址。)

main.c
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}
排順序拖動或點箭頭排順序

用 new_node(data, next) 建出 3 → 7 → 9。把這三行排好:建一個結點的時候,它的 next 要指的那個結點得已經有了。

  1. Node *b = new_node(7, c);
  2. Node *a = new_node(3, b);
  3. Node *c = new_node(9, NULL);
要顯示
3 7 9

遍歷:p = p->next 走到 NULL

走遍整個鏈結串列的寫法幾乎永遠是這三行:

p↓
3a
7b
9c
NULL

遍歷:p 從 head 出發,每次 p = p->next,走到 NULL 停
main.c
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 一個結點一個結點地往後走,算出 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}
練習 1

鏈結串列有多長

補全 length:從 head 走到 NULL,數一共幾個結點。

要顯示
4
exercise1.c
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}
輸出
點「執行」或按 Ctrl+Enter

求長度、查詢都要走

鏈結串列沒有下標:要第 i 個,只能從 head 數過去。

操作陣列鏈結串列
取第 i 個O(1)O(n):從頭走 i 步
求長度O(1)(記著 len)O(n):走一遍數(除非也記著)
查詢某個值O(n)O(n)

鏈結串列的長處在下一課:已經站在一個結點上時,在它後面插入、刪除只要 O(1)。

同樣是 3 7 9 4:上面的陣列在 0x1000 起一格挨一格;下面鏈結串列的 4 個結點散在堆的各處,靠 next 一個指一個,最後一個是 NULL

大 O 一樣的時候,實際跑起來陣列常常還更快:CPU 從記憶體讀資料時一次讀進一整段(快取行,通常 64 位元組)。陣列的下一個元素多半已經跟著讀進來了;鏈結串列的下一個結點在別處,往往要再去記憶體取一趟。

單選

鏈結串列是 head → 5 → 1 → 4 → 2。p 從 head 出發(站在 5 上),要做幾次 p = p->next 才站到 4 上?

多選可以選多個

單鏈表只記著 head。下面哪些操作要 O(n)?(可以選好幾個)

練習 2

找到它在第幾個

補全 find:返回第一個等於 x 的結點是第幾個(從 1 數起),找不到返回 0。

要顯示
3 0
exercise2.c
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}
輸出
點「執行」或按 Ctrl+Enter

用完要 free

每個結點都是 malloc 來的,用完要一個個 free。順序很關鍵:先記住 next,再 free。反過來先 free(p) 再讀 p->next,讀的就是已經還回去的記憶體。

堆疊

headNode *
nextNode *?

堆

3Node
7Node
9Node

釋放整條鏈結串列:每一輪先用 next 記住下一個,再 free 當前這個;第 2 步裡 head 變紅,就是「已經還回去,不能再讀」
單選

這樣釋放整條鏈結串列,行不行?for (Node *p = head; p != NULL; p = p->next) free(p);

練習 3

用完要還

補全 free_all:把每個結點都 free 掉,返回 free 了幾個。小心:free 之後就不能再讀它的 next 了。

要顯示
3
exercise3.c
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}
輸出
點「執行」或按 Ctrl+Enter

小結

這一課帶走

鏈結串列:結點散在堆上,靠 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);