【数据结构】双向链表中删除节点的方法实现(代码+详解)

【数据结构】双向链表中删除节点方法的实现(代码+详解)

分析

💕 在双向链表中,删除一个结点可能出现以下几种情况,取决于待删除的结点在链表中的位置:

  1. 删除头结点:

    • 如果待删除的结点是头结点,需要特殊处理,更新头结点为原头结点的后继结点,并释放原头结点的内存。
  2. 删除尾结点:

    • 如果待删除的结点是尾结点,需要特殊处理,更新尾结点为原尾结点的前驱结点,并释放原尾结点的内存。
  3. 删除中间结点:

    • 如果待删除的结点位于链表的中间位置,只需调整前驱结点和后继结点的指针,将它们连接在一起,并释放待删除结点的内存。

💕 这些情况可以进一步细分为以下几类:

  • 删除头结点

    • 头结点是唯一结点
    • 头结点后还有其他结点
  • 删除尾结点

    • 尾结点是唯一结点
    • 尾结点前还有其他结点
  • 删除中间结点


代码

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

// 定义双向链表的结点结构
typedef struct Node {
    int data;
    struct Node* prev;  // 前驱指针
    struct Node* next;  // 后继指针
} Node;

// 删除双向链表的头结点
Node* deleteHead(Node* head) {
    if (head == NULL) {
        printf("Error: Empty list\n");
        return NULL;
    }

    Node* newHead = head->next;

    if (newHead != NULL) {
        newHead->prev = NULL;
    }

    free(head);

    printf("Head node deleted successfully.\n");

    return newHead;
}

// 删除双向链表的尾结点
Node* deleteTail(Node* head) {
    if (head == NULL) {
        printf("Error: Empty list\n");
        return NULL;
    }

    if (head->next == NULL) {
        free(head);
        printf("Tail node (and the only node) deleted successfully.\n");
        return NULL;
    }

    Node* current = head;
    while (current->next->next != NULL) {
        current = current->next;
    }

    free(current->next);
    current->next = NULL;

    printf("Tail node deleted successfully.\n");

    return head;
}

// 删除双向链表的中间结点
Node* deleteMiddle(Node* head, int target) {
    if (head == NULL) {
        printf("Error: Empty list\n");
        return NULL;
    }

    Node* current = head;

    while (current != NULL && current->data != target) {
        current = current->next;
    }

    if (current == NULL) {
        printf("Error: Node with data %d not found in the list\n", target);
        return head;
    }

    if (current->prev != NULL) {
        current->prev->next = current->next;
    }

    if (current->next != NULL) {
        current->next->prev = current->prev;
    }

    free(current);

    printf("Node with data %d deleted successfully.\n", target);

    return head;
}

// 打印双向链表的内容
void printList(Node* head) {
    Node* current = head;

    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }

    printf("\n");
}

int main() {
    // 创建一个简单的双向链表:1 <-> 2 <-> 3 <-> 4
    Node* head = (Node*)malloc(sizeof(Node));
    head->data = 1;
    head->prev = NULL;
    head->next = (Node*)malloc(sizeof(Node));
    head->next->data = 2;
    head->next->prev = head;
    head->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->prev = head->next;
    head->next->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->next->data = 4;
    head->next->next->next->prev = head->next->next;
    head->next->next->next->next = NULL;

    printf("Original list: ");
    printList(head);

    // 删除头结点
    head = deleteHead(head);
    printf("List after deleting head: ");
    printList(head);

    // 删除尾结点
    head = deleteTail(head);
    printf("List after deleting tail: ");
    printList(head);

    // 删除中间结点(例如,删除值为3的结点)
    head = deleteMiddle(head, 3);
    printf("List after deleting middle node: ");
    printList(head);

    return 0;
}
相关推荐
BothSavage7 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn7 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽9 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法