news 2026/6/15 15:34:29

C语言链表的相关操作

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C语言链表的相关操作

本文实现了一个学生信息管理的单向链表系统。头文件定义了链表结构体(包含学号、姓名、成绩)和基本操作接口。源文件实现了创建/销毁链表、插入/删除/查找节点、判断空链表、获取链表长度等功能,并提供了两种格式的打印函数。测试程序演示了创建链表、添加3个学生节点、查找节点、删除节点及打印链表等操作。系统采用模块化设计,通过函数指针实现灵活的打印方式,内存管理严谨,包含错误处理机制。该链表实现可作为学生信息管理的基础数据结构。

#ifndef_LINKED_LIST_H_#define_LINKED_LIST_H_#defineMAX_NAME_LEN14typedefenumStatus{ERROR,OK,NO=0,YES}Status;typedefstructStu{intstu_id;charname[MAX_NAME_LEN];floatscore;}Stu,Data;typedefstructNode{Data data;structNode*next;}Node,*pNode,*link;//1创建一个链表linkcreate_list();//销毁一个链表voiddestroy_list(link*link);//插入一个节点Statusinsert_node(link link,Stu stu);//查找一个节点pNodefind_node(link link,intstuid);//删除一个节点Statusdelete_node(link link,intstuid);//链表是否为空Statusis_empty(link link);//链表一共有多少个节点intget_link_size(link link);//打印一个信息voidprint_one_stu1(Stu stu);voidprint_one_stu2(Stu stu);voidprint_Node(pNode node,voidprint(Stu stu));voidprint_link(link link,voidprint(Stu stu));#endif
#include"linked_list.h"#include<stdlib.h>#include<string.h>#include<stdio.h>linkcreate_list(){pNode p=(pNode)malloc(sizeof(Node)*1);if(!p){perror("malloc error~!\n");exit(-1);}p->data.stu_id=0;strcpy(p->data.name,"");p->data.score=0.0f;p->next=NULL;returnp;}//[] 1 2 3 4 5voiddestroy_list(link*link){if(*link==NULL){return;}Node*current=*link;while(current!=NULL){Node*tmp=current;current=current->next;free(tmp);}*link=NULL;return;}Statusinsert_node(link link,Stu stu){if(link==NULL){returnERROR;}pNode new_node=(pNode)malloc(sizeof(Node)*1);if(!new_node){perror("malloc error~!\n");exit(-1);}new_node->data=stu;new_node->next=NULL;pNode p=link;while(p->next!=NULL){p=p->next;}p->next=new_node;returnOK;}pNodefind_node(link link,intstuid){if(link==NULL||link->next==NULL){returnNULL;}pNode current=link->next;while(current!=NULL){if(current->data.stu_id==stuid){returncurrent;}current=current->next;}returnNULL;}Statusdelete_node(link link,intstuid){if(link==NULL&&link->next){returnERROR;}pNode current=link;while(current->next!=NULL&&current->next->data.stu_id!=stuid){current=current->next;}if(current->next==NULL){returnERROR;}Node*tmp=current->next;current->next=tmp->next;free(tmp);returnOK;}voidprint_one_stu1(Stu stu){printf("%d\t%s\t%.2f\n",stu.stu_id,stu.name,stu.score);}voidprint_one_stu2(Stu stu){printf("%d\n%s\n%.2f\n",stu.stu_id,stu.name,stu.score);}voidprint_Node(pNode node,voidprint(Stu stu)){print(node->data);}Statusis_empty(link link){if(link==NULL||link->next==NULL)returnOK;returnNO;}intget_link_size(link link){if(link==NULL||link->next==NULL)return0;size_t size=0;pNode current=link->next;while(current){size+=1;current=current->next;}returnsize;}voidprint_link(link link,voidprint(Stu stu)){if(link==NULL||link->next==NULL)return;pNode current=link->next;while(current){print(current->data);current=current->next;}return;}
#include"linked_list.h"#include<stdio.h>#include<stdlib.h>voidtest(){link link=create_list();Stu s1={1001,"libai",99.0f};//shift + alt + 下Stu s2={1002,"libai2",92.0f};Stu s3={1003,"libai3",99.0f};insert_node(link,s1);insert_node(link,s2);insert_node(link,s3);pNode p=find_node(link,1011);if(p)print_Node(p,print_one_stu2);if(p)print_Node(p,print_one_stu1);print_link(link,print_one_stu1);Status status=delete_node(link,1001);printf("%s\n",status?"成功删除":"没找到");print_link(link,print_one_stu1);destroy_list(&link);if(link!=NULL){free(link);link=NULL;}}intmain(){test();return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 16:28:40

计算机视觉:从入门到熟悉(四)

第2章 卷积2.3 二维卷积2.3.1 二维卷积的数学基础与几何直观2.3.1.1 二维卷积的严格数学定义二维卷积的定义&#xff1a; 对于两个二维函数 $f(x,y)$ 和 $g(x,y)$&#xff0c;它们的卷积定义为&#xff1a;对于离散情况&#xff0c;如果 $f$ 是 $M \times N$ 的图像&#xff0c…

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

【SSM果蔬经营平台系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案

摘要 首先,论文一开始便是清楚的论述了系统的研究内容。其次,剖析系统需求分析,弄明白“做什么”,分析包括业务分析和业务流程的分析以及用例分析,更进一步明确系统的需求。然后在明白了系统的需求基础上需要进一步地设计系统,主要包罗软件架构模式、整体功能模块、数据库设计。…

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

10 纳米已是极限?GPT-5.2 和 Gemini 3 正在“逼疯”芯片设计师!下一代 AI 模型需要的光子芯片与量子算力!

朋友们&#xff0c;我们都知道 GPT-5.2和 Gemini 3的智能程度令人咋舌&#xff0c;但它们之所以能聪明到这个地步&#xff0c;背后是数万亿次的浮点运算和天文数字的晶体管堆叠。这场 AI 竞争&#xff0c;已经将我们推向了**“后摩尔时代”最严峻的挑战&#xff1a;算力瓶颈与能…

作者头像 李华
网站建设 2026/6/14 23:04:37

3小时搞定双支付集成:FastAPI全栈项目接入Stripe与PayPal终极指南

3小时搞定双支付集成&#xff1a;FastAPI全栈项目接入Stripe与PayPal终极指南 【免费下载链接】full-stack-fastapi-postgresql tiangolo/full-stack-fastapi-postgresql: 这是一个用于构建全栈Web应用程序的Python框架&#xff0c;使用FastAPI和PostgreSQL。适合用于需要使用P…

作者头像 李华
网站建设 2026/6/15 18:00:47

量子软件测试:我们现在需要准备什么?

随着IBM、Google等科技巨头陆续实现量子霸权&#xff0c;量子计算正从理论走向工程化应用。根据Gartner预测&#xff0c;到2027年将有40%的大型企业启动量子计算项目。作为软件测试从业者&#xff0c;我们正站在传统测试与量子测试的历史交汇点。面对叠加态、量子纠缠等全新特性…

作者头像 李华