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语言中创建并操作两个节点。定义节点结构、分配内存、连接节点、插入节点、删除节点、反转链表是操作链表的基本步骤。
热门推荐
怀疑胰腺炎需要做什么检查
胰腺炎B超能否洞察?治疗与日常护理全攻略
脚底上长这种小疙瘩别大意,是跖疣,会传染
洁净室污染控制:施工过程中的污染预防策略
脚气鞋里喷什么消毒
桃木剑摆放位置风水指南:客厅、卧室、书房等空间的科学布局
宋涛:著名的马克思主义经济学家和教育家
如何预防RAID 0磁盘阵列中的数据丢失
中医儿科门诊“生长贴”受欢迎,专家提醒要规范贴敷
AISI 316不锈钢:工业应用中的高耐腐蚀性解决方案
ARV:艾滋病治疗的新希望
身边的“医”靠 | 药学服务下沉社区,守护居民用药安全
网约车合法经营全攻略:从申请到运营的法律规定
孩子学吉他有哪些好处?从音乐素养到自信心全面提升
腰椎间盘突出症可以治好吗
老师在教室打学生算违规吗
桃木剑的正确挂法及使用方法
多张照片首次曝光 南京大屠杀再添铁证
李白和道教徒元丹丘之间有什么不得不说的故事?
VBA快速提取Excel数据的实用技巧
黄金999 vs 黄金9999:哪个更好?黄金999跟黄金9999的区别?
一文详解计算机中的"字"与"字节":概念、区别及应用场景
我到底应该选多少键的键盘——机械键盘配列选择指南
辅警转正与公务员考试:关键考试科目及条件深度解析
自学物联网技术 需要掌握哪些知识?
“蚩尤”是黄帝给他的贬义称号,他的真实名字,记载在苗族文献中
网站上传流程详解:从文件准备到成功发布
藏在旅行博主分享里的向往的生活
楼上下水道漏水责任归属及处理方法详解
如何解决下水道反复堵塞的问题?这种问题的根源是什么?