news 2026/6/10 14:00:16

简单链表C语言实现实例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
简单链表C语言实现实例

代码展示

#include <stdio.h> #include <stdlib.h> //定义链表节点结构 struct Node { int data; //节点数据 struct Node* next; //指向下一节点的指针 }; //创建新节点 struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //为节点分配内存 if(newNode == NULL) { printf("内存分配失败!\n"); return NULL; } newNode->data = data; //设置节点数据 newNode->next = NULL; //设置下一节点指针为空 return newNode; } //在链表末尾添加节点 void appendNode(struct Node** head, int data) { struct Node* newNode = createNode(data); //创建新节点 if(*head == NULL) { *head = newNode; //如果链表为空,将新节点设为头节点 return; } struct Node* current = *head; while(current->next != NULL) { current = current->next; //遍历链表,找到最后一个节点 } current->next = newNode; //将新节点添加到链表末尾 } //在链表开头添加节点 void prependNode(struct Node** head, int data) { struct Node* newNode = createNode(data); //创建新节点 newNode->next = *head; *head = newNode; //将新节点设为头节点 } //删除指定值的节点 void deleteNode(struct Node** head, int data) { if(*head == NULL) { printf("链表为空!\n"); return; } struct Node* current = *head; struct Node* prev = NULL; //如果要删除的是头结点 if(current != NULL && current->data == data) { *head = current->next; free(current); printf("删除成功!\n"); return; } //查找要删除的节点 while(current != NULL && current->data != data) { prev = current; current = current->next; } if(current == NULL) { printf("未找到值为%d的节点!\n",data); return; } //从链表中删除节点 prev->next = current->next; free(current); printf("删除成功!\n"); } //查找节点 struct Node* findNode(struct Node* head, int data) { struct Node* current = head; while(current != NULL) { if(current->data == data) { return current; } current = current->next; } return NULL; } //打印链表 void printList(struct Node* head) { struct Node* current = head; if(current == NULL) { printf("链表为空!\n"); return; } printf("链表内容:"); while(current != NULL) { printf("%d", current->data); if(current->next != NULL) { printf(" -> "); } current = current->next; } printf("\n"); } //计算链表长度 int getLength(struct Node* head) { int length = 0; struct Node* current = head; while(current != NULL) { length++; current = current->next; } return length; } //释放链表内存 void freeList(struct Node** head) { struct Node* current = *head; struct Node* next; while(current != NULL) { next = current->next; free(current); current = next; } *head = NULL; printf("链表已释放!\n"); } //测试链表功能 int main() { struct Node* head = NULL; printf("=== 简单链表实现实例 ===\n\n"); //测试添加节点 printf("1.添加节点 1、2、3、4、5\n"); for(int i=1; i<=5; i++) { appendNode(&head, i); } printList(head); printf("链表长度:%d\n\n", getLength(head)); //测试在开头添加节点 printf("2.在开头添加节点 0\n"); prependNode(&head, 0); printList(head); printf("链表长度:%d\n\n", getLength(head)); //测试查找节点 printf("3.查找节点\n"); int searchValue = 3; struct Node* foundNode = findNode(head, searchValue); if(foundNode != NULL) { printf("找到节点:%d\n\n", foundNode->data); } else { printf("未找到值为:%d 的节点\n\n", searchValue); } //测试删除节点 printf("4.删除节点 3 \n"); deleteNode(&head, 3); printList(head); printf("链表长度:%d\n\n", getLength(head)); printf("5.删除节点 0 \n"); deleteNode(&head, 0); printList(head); printf("链表长度:%d\n\n", getLength(head)); //测试删除不存在的节点 printf("6.删除不存在的节点 99 \n"); deleteNode(&head, 99); printList(head); //释放链表 freeList(&head); return 0; }

运行结果

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 11:11:14

一文说清STM32如何控制蜂鸣器报警模块

从零开始&#xff1a;用STM32驱动蜂鸣器实现智能报警系统你有没有遇到过这样的场景&#xff1f;设备运行异常&#xff0c;但没人注意到屏幕上的警告图标&#xff1b;按下按键没有反馈&#xff0c;用户反复操作怀疑自己“手残”&#xff1b;安防系统触发了警报&#xff0c;却只亮…

作者头像 李华
网站建设 2026/6/5 1:13:52

1、使用用例有效收集软件需求

使用用例有效收集软件需求 1. 用例的崛起与本书更新 在当今的软件开发市场中,用例已从一种“有趣的技术”发展成为收集需求的标准实践,甚至延伸到了业务流程和服务提供等其他领域。随着这一趋势的发展,相关内容也进行了更新与完善。 相较于第一版,此次更新有诸多显著变化…

作者头像 李华
网站建设 2026/6/10 13:41:28

5、使用案例驱动的需求收集:原理、工具与应用

使用案例驱动的需求收集:原理、工具与应用 1. 使用案例的广泛应用 使用案例的应用范围极为广泛,并非仅仅局限于需求收集。以下是使用案例在一些可能未曾想到的领域的应用: - 仅查询系统 :对于与外界有交互的系统,使用案例都很有意义,计算机系统通常都需要与外界交互…

作者头像 李华
网站建设 2026/6/10 5:28:02

零基础掌握arm64-v8a下的NEON指令加速开发

零基础也能上手&#xff1a;arm64-v8a下的NEON指令加速实战指南你有没有遇到过这样的场景&#xff1f;写好的图像处理算法在PC上跑得飞快&#xff0c;一放到手机上却卡成PPT&#xff1b;或者一段音频滤波代码明明逻辑很简单&#xff0c;CPU占用率却飙到80%以上。问题出在哪&…

作者头像 李华
网站建设 2026/6/2 11:25:15

11、神经网络构建与训练:从架构选择到高效训练策略

神经网络构建与训练:从架构选择到高效训练策略 1. 神经网络架构的选择与比较 1.1 额外隐藏层的优势 在构建神经网络时,尝试不同的架构是很有必要的。例如改变层数、神经元数量以及权重初始化方式等。虽然理论上单层网络可以近似任何函数,但所需的神经元数量可能非常庞大,…

作者头像 李华
网站建设 2026/6/10 12:39:46

2、安卓应用使用全攻略

安卓应用使用全攻略 1 安卓应用指南简介 在探索安卓应用的世界时,你无需按部就班地从头开始了解。你可以直接跳到你感兴趣的特定应用章节进行深入了解。这里可以把它看作是一个超棒的安卓应用参考指南。除了“理解安卓”这一章节不涉及具体应用评测外,其余 18 个章节分别对…

作者头像 李华