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);