news 2026/6/15 16:47:59

简单链表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/15 10:23:23

downkyi视频下载终极指南:轻松获取B站超高清资源

downkyi视频下载终极指南&#xff1a;轻松获取B站超高清资源 【免费下载链接】downkyi 哔哩下载姬downkyi&#xff0c;哔哩哔哩网站视频下载工具&#xff0c;支持批量下载&#xff0c;支持8K、HDR、杜比视界&#xff0c;提供工具箱&#xff08;音视频提取、去水印等&#xff09…

作者头像 李华
网站建设 2026/6/15 9:31:13

PlantUML编辑器:5分钟掌握文本绘图革命

PlantUML编辑器&#xff1a;5分钟掌握文本绘图革命 【免费下载链接】plantuml-editor PlantUML online demo client 项目地址: https://gitcode.com/gh_mirrors/pl/plantuml-editor 还在为复杂的UML图表绘制而烦恼吗&#xff1f;PlantUML Editor作为一款基于文本的UML绘…

作者头像 李华
网站建设 2026/6/15 10:23:26

GPT-SoVITS在教育领域的应用场景探索

GPT-SoVITS在教育领域的应用场景探索 在一所偏远山区的中学课堂上&#xff0c;学生们正通过平板电脑收听一段讲解&#xff1a;“同学们好&#xff0c;今天我们来学习勾股定理。”声音温和清晰&#xff0c;语调自然&#xff0c;仿佛是他们熟悉的数学老师在讲课。但事实上&#x…

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

11、整体迭代/增量软件生命周期原则解析

整体迭代/增量软件生命周期原则解析 在软件项目管理中,整体迭代/增量(HI/I)软件生命周期遵循着一系列通用原则,这些原则对于实现有效的项目管理至关重要。下面我们将详细探讨这些原则及其在项目管理中的应用。 1. 管理需求而非任务 在 HI/I 生命周期中,项目经理对项目需…

作者头像 李华
网站建设 2026/6/15 10:23:03

5大核心功能,这款英雄联盟辅助工具让游戏体验全面升级

5大核心功能&#xff0c;这款英雄联盟辅助工具让游戏体验全面升级 【免费下载链接】LeagueAkari ✨兴趣使然的&#xff0c;功能全面的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/LeagueAkari 还在为繁…

作者头像 李华
网站建设 2026/6/15 10:24:32

15、深度学习中的正则化方法:ℓ1与ℓ2正则化详解

深度学习中的正则化方法:ℓ1与ℓ2正则化详解 1. 复杂度概念 复杂度(并非特指网络复杂度)源于信息论。例如,非零权重的数量会随着训练轮数、优化算法等因素显著变化,这意味着复杂度这一模糊直观的概念还依赖于模型的训练时长。理论上,网络复杂度是一个极难精确定义的概念…

作者头像 李华