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语言中创建并操作两个节点。定义节点结构、分配内存、连接节点、插入节点、删除节点、反转链表是操作链表的基本步骤。
热门推荐
高血压患者要少吃米饭?医生:不想造成胰岛素抵抗,就别这么吃!
探索多媒体在英语教育中的应用研究
超850万人被纳入失信被执行人名单
健康不佳时,是否适合前往扫墓?
腹部B超检查内容及注意事项全解析
闽台青年在福建古田“沉浸式”体验国家级非遗陈靖姑信俗文化
冬季洗澡,皮肤干燥怎么办?这5个护肤细节请收好→
银杏叶有什么好处
后背筋膜炎症状及治疗方法
故意违约:如何根据合同条款解除合同
如何正确清洗雪地靴(掌握正确的清洗方法)
丽水市旅游景点攻略,探寻自然之美,体验文化韵味
刘邦分封的八大异姓王揭秘
刘禅的母亲是哪位夫人?甘夫人是刘禅的母亲
揭秘三国谜团:刘禅之母究竟是甘夫人还是糜夫人?
威斯康星州有哪些大城市
两度落选双一流,也打不倒这所财经大学?谈江西财经大学的没落史
纪录片《叶尔羌河》:自然美、文化韵与民族情
《文明》系列:从经典传奇到新时代挑战的策略演进
教科书级满墙电视柜设计:小户型必看的空间扩容与颜值秘籍
皮肤长红斑,不一定是湿疹,还有这几种可能
换季眼睛超痒?季节性过敏性结膜炎的预防与治疗
通力电梯优势及故障率最高的电梯品牌分析
《沉默的真相》:一部关于正义与信念的悬疑佳作
日本研究:血压升高,问题出在晚饭上?提醒:晚饭牢记"两不 ...
学会这5种修辞技巧,让你的文章更具艺术感
氢能源动力汽车品牌大盘点:续航里程、加氢时间全解析
住宅风水自我调整技巧,提升居住环境
从“金牌学生”到“金牌教师”:一个中职生的7年蝶变之路
没有时间的世界,该如何描述?