How to delete a specified node in a doubly linked list in the C language?

To remove a node from a doubly linked list, the following steps need to be executed:

  1. Firstly, check if the linked list is empty. If it is, then the node cannot be deleted and simply return.
  2. Traverse through the linked list to locate the node that needs to be deleted. One approach is to use a pointer to point to the current node and iterate through the list until the target node is found or the end of the list is reached.
  3. If the node to be deleted is the first node in the linked list, which means the pointer pointing to this node is the head pointer, then set the head pointer to point to the next node, and free the memory space of the deleted node.
  4. If the node to be deleted is the last one in the linked list, set the next pointer of the previous node to NULL and free the memory space of that node.
  5. If the node to be deleted is a middle node in the linked list, then the next pointer of the previous node should be directed to the next node of the current one, and at the same time, the previous pointer of the next node should be directed to the previous node of the current one. Finally, the memory space of the current node should be released.
  6. If the desired node is not found after traversing the entire linked list, it means that the node does not exist in the linked list, so just return.

Here is an example code implementation:

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

// 双向链表节点结构体
typedef struct Node {
    int data;
    struct Node *prev;  // 指向前一个节点的指针
    struct Node *next;  // 指向后一个节点的指针
} Node;

// 删除节点函数
void deleteNode(Node **head, int value) {
    if (*head == NULL) {
        printf("链表为空,无法删除节点\n");
        return;
    }

    Node *current = *head;
    while (current != NULL) {
        if (current->data == value) {
            if (current == *head) {
                // 要删除的节点是头节点
                *head = current->next;
                if (*head != NULL) {
                    (*head)->prev = NULL;
                }
                free(current);
            } else if (current->next == NULL) {
                // 要删除的节点是尾节点
                current->prev->next = NULL;
                free(current);
            } else {
                // 要删除的节点是中间节点
                current->prev->next = current->next;
                current->next->prev = current->prev;
                free(current);
            }
            printf("成功删除节点\n");
            return;
        }
        current = current->next;
    }

    printf("未找到要删除的节点\n");
}

// 打印链表函数
void printList(Node *head) {
    Node *current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    Node *head = NULL;  // 链表头指针

    // 创建链表
    Node *node1 = (Node *)malloc(sizeof(Node));
    node1->data = 1;
    node1->prev = NULL;
    node1->next = NULL;
    head = node1;

    Node *node2 = (Node *)malloc(sizeof(Node));
    node2->data = 2;
    node2->prev = node1;
    node2->next = NULL;
    node1->next = node2;

    Node *node3 = (Node *)malloc(sizeof(Node));
    node3->data = 3;
    node3->prev = node2;
    node3->next = NULL;
    node2->next = node3;

    // 打印原始链表
    printf("原始链表:");
    printList(head);

    // 删除节点
    deleteNode(&head, 2);

    // 打印删除节点后的链表
    printf("删除节点后的链表:");
    printList(head);

    return 0;
}

In this example, a doubly linked list with three nodes is first created. The deleteNode function is then called to remove the node with a value of 2. Finally, the updated linked list is printed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *