跳到内容

链表 ​

为什么需要链表 ​

数组的大小在定义时就固定了,想中途增删元素很麻烦——中间插入要把后面所有元素搬动。链表用分散的节点解决这个问题:每个节点存一份数据和一个指向下一个节点的指针,节点在内存里不必连续,靠指针串成一条链。要插入或删除,只需改几个指针,不用搬动大片数据。

单链表节点 ​

一个单链表节点包含两部分:数据域和指针域。指针域指向下一个节点,最后一个节点的指针指向 NULL,表示链的终点。

c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *node_create(int data)
{
    Node *n = malloc(sizeof(Node));
    if (n == NULL) {
        fprintf(stderr, "内存分配失败\n");
        exit(1);
    }
    n->data = data;
    n->next = NULL;
    return n;
}

int main(void)
{
    Node *a = node_create(10);
    Node *b = node_create(20);
    a->next = b;
    printf("a->data = %d, a->next->data = %d\n", a->data, a->next->data);
    free(a);
    free(b);
    return 0;
}

输出:

text
a->data = 10, a->next->data = 20

struct Node 里有一个指向自身的指针 struct Node *next——这叫自引用结构,合法是因为指针的大小固定,编译器不需要知道结构体的完整布局就能确定指针的尺寸。node_create 用 malloc 动态分配一个节点,检查返回值非空后填好数据和指针。两个节点手动串起来:a->next = b,于是从 a 能走到 b。

注:结构体内部不能直接包含自身实例(struct Node { struct Node n; } 会无限嵌套),但可以包含指向自身的指针,因为指针大小是固定的。

创建、遍历与释放 ​

把节点串成完整的链表,然后遍历打印、最后逐个释放。下面这个程序演示一套完整的链表操作:在头部插入节点、遍历打印、释放整条链:

c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *list_prepend(Node *head, int data)
{
    Node *n = malloc(sizeof(Node));
    n->data = data;
    n->next = head;
    return n;
}

void list_print(Node *head)
{
    Node *cur = head;
    while (cur != NULL) {
        printf("%d -> ", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}

void list_free(Node *head)
{
    Node *cur = head;
    while (cur != NULL) {
        Node *tmp = cur;
        cur = cur->next;
        free(tmp);
    }
}

int main(void)
{
    Node *head = NULL;
    head = list_prepend(head, 10);
    head = list_prepend(head, 20);
    head = list_prepend(head, 30);
    list_print(head);
    list_free(head);
    return 0;
}

输出:

text
30 -> 20 -> 10 -> NULL

list_prepend 把新节点插到链表头部:新节点的 next 指向原来的头,然后新节点成为新的头。依次插入 10、20、30 后,链表是 30 -> 20 -> 10——后插的在前面,因为每次都是头插。

遍历用一个 cur 指针从头走到尾,每步 cur = cur->next 前进一格,直到 cur 变成 NULL。list_free 必须在释放当前节点之前,先用 tmp 记住它,再把 cur 前进到下一个,最后 free(tmp)——顺序不能反,否则释放了节点就找不到下一个了。

尾部插入:遍历到链的末端 ​

头插最简单,但有时要保持插入顺序,就要插到尾部。为此需要从头部遍历到 next 为 NULL 的最后一个节点:

c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *list_append(Node *head, int data)
{
    Node *n = malloc(sizeof(Node));
    n->data = data;
    n->next = NULL;
    if (head == NULL)
        return n;
    Node *cur = head;
    while (cur->next != NULL)
        cur = cur->next;
    cur->next = n;
    return head;
}

void list_print(Node *head)
{
    Node *cur = head;
    while (cur != NULL) {
        printf("%d -> ", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}

void list_free(Node *head)
{
    Node *cur = head;
    while (cur != NULL) {
        Node *tmp = cur;
        cur = cur->next;
        free(tmp);
    }
}

int main(void)
{
    Node *head = NULL;
    head = list_append(head, 10);
    head = list_append(head, 20);
    head = list_append(head, 30);
    list_print(head);
    list_free(head);
    return 0;
}

输出:

text
10 -> 20 -> 30 -> NULL

list_append 先处理空链表的特殊情况(直接返回新节点作为头),否则用 while (cur->next != NULL) 走到最后一个节点,再把新节点挂到它的 next 上。注意循环条件是 cur->next != NULL 而非 cur != NULL——前者停在最后一个节点上,后者会走出链表变成 NULL,无法再挂新节点。尾插的代价是每次都要遍历整条链,链长时开销大,工程上常额外维护一个尾指针来避免遍历。

要点 ​

  • 单链表节点含数据域和指向下一节点的指针,末节点指向 NULL。
  • 自引用结构只能包含指向自身的指针,不能包含自身实例。
  • 头插最简单:新节点指向旧头,成为新头;尾插需遍历到末端。
  • 遍历用 cur = cur->next 逐格前进,直到 NULL。
  • 释放链表要先记住当前节点、再前进、后释放,顺序不能反。