【初阶数据结构】链表的柔光之美

目录

一、为什么需要链表?

二、链表与数组的对比

三、链表节点定义

四、链表基本操作

[1. 创建链表](#1. 创建链表)

[2. 插入节点](#2. 插入节点)

头插法(时间复杂度O(1))

尾插法(时间复杂度O(n))

[3. 删除节点](#3. 删除节点)

[4. 遍历链表](#4. 遍历链表)

五、进阶操作

[1. 反转链表(迭代法)](#1. 反转链表(迭代法))

[2. 检测环(快慢指针法)](#2. 检测环(快慢指针法))

六、内存管理要点

七、常见错误排查

八、链表变体

十、完整示例代码

总结

路过的佬们点点关注哦~

你们的鼓励是我前进的动力~


一、为什么需要链表?

在C语言程序设计中,数组是最基础的数据结构,但它存在明显的局限性:

  • 固定长度,无法动态扩展

  • 插入/删除元素需要大量数据移动

  • 内存空间要求连续

链表(Linked List)通过动态内存分配指针连接完美解决了这些问题。每个元素(节点)包含:

  1. 数据域 - 存储实际数据

  2. 指针域 - 存储下一个节点的地址

二、链表与数组的对比

三、链表节点定义

复制代码
typedef struct Node {
    int data;           // 数据域
    struct Node *next;  // 指针域
} Node;

四、链表基本操作

1. 创建链表

复制代码
Node* createList(int data) {
    Node* head = (Node*)malloc(sizeof(Node));
    if (head == NULL) {
        printf("内存分配失败!");
        exit(1);
    }
    head->data = data;
    head->next = NULL;
    return head;
}

2. 插入节点

头插法(时间复杂度O(1))
复制代码
void insertAtHead(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = *head;
    *head = newNode;
}
尾插法(时间复杂度O(n))
复制代码
void insertAtTail(Node* head, int data) {
    Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    current->next = newNode;
}

3. 删除节点

复制代码
void deleteNode(Node** head, int key) {
    Node *temp = *head, *prev;
    
    // 删除头节点
    if (temp != NULL && temp->data == key) {
        *head = temp->next;
        free(temp);
        return;
    }

    // 查找待删除节点
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) return;

    // 解除链接并释放内存
    prev->next = temp->next;
    free(temp);
}

4. 遍历链表

复制代码
void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

五、进阶操作

1. 反转链表(迭代法)

复制代码
void reverseList(Node** head) {
    Node *prev = NULL;
    Node *current = *head;
    Node *next = NULL;
    
    while (current != NULL) {
        next = current->next;  // 保存下一个节点
        current->next = prev;  // 反转指针
        prev = current;        // 前移prev
        current = next;        // 前移current
    }
    *head = prev;
}

2. 检测环(快慢指针法)

复制代码
int hasCycle(Node *head) {
    Node *slow = head, *fast = head;
    
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
        
        if (slow == fast) {
            return 1;  // 存在环
        }
    }
    return 0;  // 无环
}

六、内存管理要点

必须检查malloc返回值

复制代码
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
    // 处理内存分配失败
}

及时释放内存

复制代码
void freeList(Node* head) {
    Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

避免野指针

复制代码
free(temp);
temp = NULL;  // 释放后立即置空

七、常见错误排查

  1. 段错误(Segmentation Fault)

    • 访问已释放的内存

    • 指针未初始化就使用

  2. 内存泄漏

    • 使用valgrind工具检测

    • 确保每个malloc都有对应的free

  3. 逻辑错误

    • 忘记更新头指针

    • 指针操作顺序错误

八、链表变体

双向链表

复制代码
typedef struct DNode {
    int data;
    struct DNode *prev;
    struct DNode *next;
} DNode;

循环链表

复制代码
// 尾节点指向头节点
head->next = head;

带头节点的链表

  • 统一操作逻辑

  • 简化边界条件处理

九、应用场景

  1. 实现栈/队列

  2. 多项式运算

  3. 文件系统目录结构

  4. 图结构的邻接表

  5. 内存管理系统

十、完整示例代码

复制代码
#include <stdio.h>
#include <stdlib.h>

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

// [此处插入上述各个函数实现]

int main() {
    Node* list = createList(5);
    insertAtHead(&list, 2);
    insertAtTail(list, 8);
    
    printf("原始链表: ");
    printList(list);  // 输出: 2 -> 5 -> 8 -> NULL
    
    reverseList(&list);
    printf("反转后: ");
    printList(list);  // 输出: 8 -> 5 -> 2 -> NULL
    
    deleteNode(&list, 5);
    printf("删除后: ");
    printList(list);  // 输出: 8 -> 2 -> NULL
    
    freeList(list);
    return 0;
}

总结

链表是C语言中最基础也最重要的数据结构之一,掌握链表需要理解:

  • 指针的操作原理

  • 动态内存管理机制

  • 数据结构与算法的关系

建议通过以下方式巩固学习:

  1. 手写实现所有链表操作

  2. 使用调试工具观察内存变化

  3. 尝试实现双向链表等变体

  4. 解决LeetCode链表相关题目(如206反转链表、141环形链表)

掌握链表将为学习更复杂的数据结构(树、图等)打下坚实基础。

路过的佬们点点关注哦~

你们的鼓励是我前进的动力~

相关推荐
he___H5 小时前
数据结构-移位
数据结构
云知谷5 小时前
【C++基本功】C++适合做什么,哪些领域适合哪些领域不适合?
c语言·开发语言·c++·人工智能·团队开发
电子_咸鱼6 小时前
LeetCode——Hot 100【电话号码的字母组合】
数据结构·算法·leetcode·链表·职场和发展·贪心算法·深度优先
仰泳的熊猫6 小时前
LeetCode:785. 判断二分图
数据结构·c++·算法·leetcode
haoly19898 小时前
数据结构和算法篇-归并排序的两个视角-迭代和递归
数据结构·算法·归并排序
小梁努力敲代码8 小时前
java数据结构--List的介绍
java·开发语言·数据结构
保持低旋律节奏10 小时前
C++——list链表
c++·链表·list
Code小翊11 小时前
归并排序基础理解
数据结构·算法·排序算法
.小小陈.11 小时前
数据结构2:单链表
c语言·开发语言·数据结构·笔记·学习方法
草莓工作室11 小时前
数据结构4:线性表3-链式存储的线性表
数据结构