news 2026/5/26 22:00:58

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/5/22 9:47:01

Graphiti知识图谱构建与AI集成实战指南:从零搭建智能记忆系统

Graphiti知识图谱构建与AI集成实战指南&#xff1a;从零搭建智能记忆系统 【免费下载链接】graphiti 用于构建和查询时序感知知识图谱的框架&#xff0c;专为在动态环境中运行的 AI 代理量身定制。 项目地址: https://gitcode.com/GitHub_Trending/grap/graphiti 你是否…

作者头像 李华
网站建设 2026/5/22 2:06:24

彻底解决大型前端项目痛点:umi模块化拆分与联邦架构完全指南

彻底解决大型前端项目痛点&#xff1a;umi模块化拆分与联邦架构完全指南 【免费下载链接】umi A framework in react community ✨ 项目地址: https://gitcode.com/GitHub_Trending/um/umi 你是否正面临这样的困境&#xff1a;前端项目越来越庞大&#xff0c;构建时间从…

作者头像 李华
网站建设 2026/5/24 12:50:39

Vuetify日历组件终极指南:7天从零打造专业日程管理系统

Vuetify日历组件终极指南&#xff1a;7天从零打造专业日程管理系统 【免费下载链接】vuetify &#x1f409; Vue Component Framework 项目地址: https://gitcode.com/gh_mirrors/vu/vuetify 还在为复杂的日程管理功能发愁吗&#xff1f;Vuetify的VCalendar组件让这一切…

作者头像 李华
网站建设 2026/5/25 0:11:55

《Agentic设计模式》:构建智能系统的实战指南

本文系统介绍AI智能体的概念、五步循环工作法及四个复杂度层级&#xff0c;详细阐述构建智能体系统的21个核心设计模式&#xff0c;涵盖基础模式、高级能力、鲁棒性和系统级模式。同时探讨智能体未来五大假设、市场趋势及实践建议&#xff0c;为开发者提供从理论到实践的完整指…

作者头像 李华