问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C语言如何创建两个节点

创作时间:
作者:
@小白创作中心

C语言如何创建两个节点

引用
1
来源
1.
https://docs.pingcode.com/baike/1037254

在C语言中创建两个节点的步骤:定义节点结构、分配内存、连接节点。首先,我们定义一个节点结构,接着通过动态内存分配函数分配内存,最后将两个节点相互连接。下面详细介绍如何实现。

一、定义节点结构

在C语言中,节点通常是通过结构体来定义的。一个节点结构体包含数据部分和指向下一个节点的指针。

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

这个结构体包含了一个整数类型的数据成员和一个指向下一个节点的指针next

二、分配内存

要创建节点,首先需要为结构体分配内存。我们可以使用C语言中的malloc函数来进行动态内存分配。

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

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

int main() {
    // 创建第一个节点
    struct Node* first = (struct Node*)malloc(sizeof(struct Node));
    // 创建第二个节点
    struct Node* second = (struct Node*)malloc(sizeof(struct Node));
    // 检查内存分配是否成功
    if (first == NULL || second == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    // 为节点赋值
    first->data = 1;
    second->data = 2;
    // 将第一个节点的 next 指向第二个节点
    first->next = second;
    // 将第二个节点的 next 设为 NULL,表示链表结束
    second->next = NULL;
    // 打印链表中的数据
    printf("First node data: %d\n", first->data);
    printf("Second node data: %d\n", second->data);
    // 释放内存
    free(first);
    free(second);
    return 0;
}

三、连接节点

在上面的例子中,我们已经将第一个节点的next指针指向了第二个节点。接下来,我们来看如何更好地组织这些节点。

// 将第一个节点的 next 指向第二个节点
first->next = second;
// 打印链表中的数据
struct Node* current = first;
while (current != NULL) {
    printf("Node data: %d\n", current->data);
    current = current->next;
}

通过这种方式,我们可以遍历整个链表并输出每个节点中的数据。

四、内存管理

在C语言中,手动内存管理是必要的。我们必须为每个节点分配内存,并在不再需要时释放它。

// 释放内存
free(first);
free(second);

五、扩展实现

现在,我们已了解了如何创建和连接两个节点,接下来我们将扩展这一实现,使其能够支持更多节点和其他功能。

1、插入节点

我们可以编写函数来插入新节点到链表的任何位置。

void insertAfter(struct Node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("The given previous node cannot be NULL");
        return;
    }
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}

2、删除节点

删除节点同样需要特别注意内存管理。

void deleteNode(struct Node head_ref, int key) {
    struct Node* temp = *head_ref, *prev;
    if (temp != NULL && temp->data == key) {
        *head_ref = 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);
}

3、反转链表

反转链表是一种常见操作,它可以通过调整指针的方向来实现。

void reverse(struct Node head_ref) {
    struct Node* prev = NULL;
    struct Node* current = *head_ref;
    struct Node* next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head_ref = prev;
}

六、综合示例

下面是一个综合示例,它展示了如何创建、连接、插入、删除和反转节点:

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

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

void insertAfter(struct Node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("The given previous node cannot be NULL");
        return;
    }
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}

void deleteNode(struct Node head_ref, int key) {
    struct Node* temp = *head_ref, *prev;
    if (temp != NULL && temp->data == key) {
        *head_ref = 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);
}

void reverse(struct Node head_ref) {
    struct Node* prev = NULL;
    struct Node* current = *head_ref;
    struct Node* next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head_ref = prev;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf(" %d ", node->data);
        node = node->next;
    }
}

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("Original list: ");
    printList(head);
    insertAfter(second, 4);
    printf("\nList after insertion: ");
    printList(head);
    deleteNode(&head, 2);
    printf("\nList after deletion: ");
    printList(head);
    reverse(&head);
    printf("\nReversed list: ");
    printList(head);
    return 0;
}

七、总结

通过上述步骤,我们已经能够在C语言中创建并操作两个节点。定义节点结构、分配内存、连接节点、插入节点、删除节点、反转链表是操作链表的基本步骤。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号
C语言如何创建两个节点