📜  将节点插入链表的中间(1)

📅  最后修改于: 2023-12-03 14:53:55.089000             🧑  作者: Mango

在链表中间插入节点

在链表中插入一个新的节点是一项常见的任务。通常情况下,我们在链表的开头或结尾插入节点,但是有时候,我们希望将节点插入链表的中间,比如在某个特定节点之后或之前。在本文中,我们将介绍如何在链表的中间插入一个节点。

初始链表

首先,我们需要创建一个初始链表,用于演示如何在链表中间插入节点。下面是一个包含三个节点的链表:

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

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

void printList(struct Node* head) {
    struct Node* temp = head;

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

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

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

    return 0;
}

输出结果为:

Initial list: 1 2 3 
在链表中间插入节点

现在,我们将演示如何在链表中间插入一个节点。假设我们希望将一个新的节点插入到第二个节点之后。首先,我们需要创建一个新的节点:

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 4;
newNode->next = NULL;

然后,我们需要遍历链表,找到需要插入新节点的位置。在这个例子里,我们需要找到第二个节点,然后将新节点插入到它的后面。我们可以用一个指针变量 current 来遍历链表,同时使用另一个指针变量 previous 来跟踪当前节点的前面一个节点。

struct Node* current = head;
struct Node* previous = NULL;

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

在这段代码中,我们将 current 指针初始化为链表的第一个节点,将 previous 指针初始化为 NULL。然后,我们使用 while 循环来遍历链表,直到达到最后一个节点或找到我们要插入新节点的位置。在循环中,我们将 previous 指针移到 current 所指向的节点,并将 current 指针移到下一个节点。

当我们找到了需要插入新节点的位置时,我们只需要将新节点插入到 previouscurrent 之间即可:

newNode->next = current;
previous->next = newNode;

最后,我们可以用 printList 函数来输出链表,以验证新节点是否已经成功插入到了链表中间。完整的代码如下:

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

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

void printList(struct Node* head) {
    struct Node* temp = head;

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

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

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

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = 4;
    newNode->next = NULL;

    struct Node* current = head;
    struct Node* previous = NULL;

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

    if (current == NULL) {
        printf("Error: The node with data value of 2 is not in the list!\n");
        return 1;
    }

    newNode->next = current;
    previous->next = newNode;

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

    return 0;
}

输出结果为:

Initial list: 1 2 3 
Updated list: 1 2 4 3 

我们可以看到,已经成功将新节点插入到了链表的中间。