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语言中创建并操作两个节点。定义节点结构、分配内存、连接节点、插入节点、删除节点、反转链表是操作链表的基本步骤。
热门推荐
山西医科大学康永波团队总结了益生菌和益生元治疗溃疡性结肠炎的可能机制及其前景
探寻数字化非遗传承之路:歙县鱼灯文化的守护与创新
合工大学子深入汪满田村,探索非遗鱼灯文化振兴之路
传承与创新:瞻淇鱼灯“游”在故乡的路上
耳石症反复发作惹人恼,补充维生素D有助于减少复发
算网融合下的多云部署和数据存储发展趋势分析
瑜伽真的能缓解头痛吗?专家解读来了!
中国首部偏头痛急性期治疗指南发布,CGRP受体拮抗剂成治疗新选择
头痛还是偏头痛?教你正确区分和应对
南瓜子:偏头痛患者的天然良方
压力管理:提升胰岛素敏感性的关键
低脂素食饮食模式:改善胰岛素敏感性的新选择
乔榛谈译制片的工艺流程和精品创作
揭秘《哪吒之魔童降世》幕后声音:配音演员大揭秘!
敏捷开发中的迭代:6个成功案例解析
快速增进人际关系的实用指南
心理健康:如何影响你的人际关系?
职场新人必修课:如何处理复杂的人际关系
延吉市科目三考试路线大揭秘!
延边第二人民医院骨科:23年专注关节置换,专家团队实力雄厚
动感且浪漫,新生代女性花式公益引人注目
横跨70年,追踪268位哈佛学生,他发现命运的分水岭原来是…
冰雪奇缘vs幽灵公主:谁家公主更厉害?
《西游:笔绘西行》孔雀公主技能大揭秘!
最美“昔阳”红 文旅产业助力县域经济“破壁出圈”
冰天雪地也是金山银山——推动冰雪经济成为新增长点
非遗篆刻技艺进校园:广州华立科技职业学院与广州牧心印社共探产教融合新路径
小说写作入门:掌握节奏与细节,让你的小说打斗场景跃然纸上
金庸笔下有门奇功,正经去学学不会,用错误的方式去学反而能练成
政治上的成熟:情绪、人情世故与思想的平衡术!