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语言中创建并操作两个节点。定义节点结构、分配内存、连接节点、插入节点、删除节点、反转链表是操作链表的基本步骤。
热门推荐
玫瑰花的象征意义及文化传承(解读玫瑰花的丰富含义与不同文化中的象征意义)
《黑神话悟空》人物剧情梳理分析:灵吉菩萨的多重身份
砸墙前必知!承重墙与剪力墙的差异,关乎全家安危!
室外装修别忽视!美国房子装修设计提升房产价值的秘诀
斗兽棋:经典策略棋类游戏完全攻略
谁才是最强125踏板?铃木UY和豪爵AFR谁才是125踏板之王!
入耳式耳机和半入耳式耳机哪个好
墙面刷乳胶漆好还是贴墙纸好?听行内人一分析,再也不纠结了
孩子第一次配镜,到底该怎么选?
《洛菲斯的呼唤》玩法攻略:从入门到精通
境外公司合同签署要求:以美国公司和香港公司为例
纳米技术在肿瘤免疫治疗中的应用
婚恋诈骗防范知识讲座
劳动仲裁的具体步骤有哪些?
赏漫山樱花、泡能喝的温泉,福建清流将山水资源变为浪漫旅游新潮流
《舟话古桥》之二:中国四大古桥之卢沟桥
马力大就一定快?揭秘扭矩与马力的真实关系
哪吒启示录:品质为王、恒心铸魂与传统焕新——电影背后的深层思考
喜欢天文的你,也有机会发现新星!教程来了
为什么都在吃优甲乐,却很少吃雷替斯?
不孝行为应受到批评与谴责:构建和谐家庭和社会的基础
埃及尼罗河流
自媒体平台如何选择,适合视频多平台发布吗?
植物的生长与环境因素
车辆信息查询指南:车五项查询包含什么?
蓝牙5.0无线耳机连接稳定性解析:提升传输速度与覆盖范围
豆瓣9.1的好东西,让品牌看到「治愈」新机遇
5G-A商用元年开启!华为联手多家运营商,将撬动哪些新商机
脊针治疗混合型颈椎病的临床研究
家长们,请适度放手