news 2026/2/4 7:03:39

简单链表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/2/3 5:33:06

Proteus下载核心要点:快速理解教育仿真平台搭建

从零搭建电子教学实验室&#xff1a;Proteus仿真平台实战指南 你有没有遇到过这样的场景&#xff1f; 一节单片机实验课上&#xff0c;一半学生在排队等开发板&#xff0c;另一半则因为接错线烧了芯片被迫“旁听”&#xff1b;老师一边修设备一边讲原理&#xff0c;教学节奏支…

作者头像 李华
网站建设 2026/2/3 5:45:32

闲鱼自动化神器终极指南:3步实现无人值守运营

想要彻底解放双手&#xff0c;让闲鱼店铺自动运行吗&#xff1f;xianyu_automatize 这款免费开源工具就是你的最佳选择&#xff01;作为一款专注于闲鱼平台的自动化神器&#xff0c;它能帮你自动完成签到、批量擦亮宝贝、统计运营数据等重复性工作&#xff0c;每天至少节省1小时…

作者头像 李华
网站建设 2026/2/3 23:49:18

语音克隆成本有多低?GPT-SoVITS经济性分析

语音克隆成本有多低&#xff1f;GPT-SoVITS经济性分析 在不到两年前&#xff0c;想要克隆一个人的声音&#xff0c;至少需要30分钟以上的高质量录音、一支算法团队和数万元的云训练费用。如今&#xff0c;一个普通用户只需用手机录下一分钟清晰语音&#xff0c;就能在自家电脑上…

作者头像 李华
网站建设 2026/2/3 3:39:26

终极企业年会抽奖系统:快速部署完整指南

终极企业年会抽奖系统&#xff1a;快速部署完整指南 【免费下载链接】lucky-draw 年会抽奖程序 项目地址: https://gitcode.com/gh_mirrors/lu/lucky-draw 企业年会抽奖系统是现代企业庆典活动中不可或缺的在线抽奖工具&#xff0c;这款企业抽奖软件能够帮助您快速搭建专…

作者头像 李华
网站建设 2026/2/4 3:47:36

RS485通信基础与STM32配置入门必看

从零搞懂RS485&#xff1a;STM32实战配置与工业通信避坑指南你有没有遇到过这样的场景&#xff1f;一个传感器网络&#xff0c;几台设备分布在车间的不同角落&#xff0c;距离动辄上百米。你想用串口把它们连起来&#xff0c;结果发现普通UART通信一跑就丢数据&#xff0c;噪声…

作者头像 李华
网站建设 2026/2/3 10:22:35

GHelper深度体验:华硕ROG笔记本的终极优化方案

GHelper深度体验&#xff1a;华硕ROG笔记本的终极优化方案 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops. Control tool for ROG Zephyrus G14, G15, G16, M16, Flow X13, Flow X16, TUF, Strix, Scar and other models 项目地址: http…

作者头像 李华