news 2026/7/4 21:58:47

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/7/3 19:48:50

6、网络服务枚举与安全防护全解析

网络服务枚举与安全防护全解析 1. 基础横幅抓取 横幅抓取是最基本的枚举技术,通过连接远程应用并观察输出,攻击者可获取运行服务的品牌和型号等关键信息,为漏洞研究提供线索。常见的手动横幅抓取工具包括 telnet 和 netcat 。 - telnet :大多数操作系统内置的远…

作者头像 李华
网站建设 2026/7/2 3:38:07

8、Windows系统认证攻击与防范全解析

Windows系统认证攻击与防范全解析 在Windows系统的安全领域,一旦攻击者获得了一定程度的访问权限,后续往往会展开一系列更具威胁性的行动。本文将详细介绍攻击者在获得访问权限后可能采取的攻击手段,以及相应的防范措施。 1. 权限提升 攻击者获取Windows系统的用户账户后…

作者头像 李华
网站建设 2026/7/2 7:41:44

Linux网络参数:现代内核的智能优化之道

你是否曾经花费大量时间调整各种网络参数&#xff0c;却发现效果甚微&#xff1f;或者盲目跟随网上的调优指南&#xff0c;却导致系统稳定性问题&#xff1f;今天&#xff0c;让我们重新审视Linux网络参数的真正价值——现代内核已经内置了令人惊叹的智能优化机制。 【免费下载…

作者头像 李华
网站建设 2026/7/1 7:42:27

C# 随机数添加数组对象和数组的查询方法

一、数组对象添加数据使用随机数案例&#xff1a;定义一个数组&#xff0c;存储10个People对象&#xff08;姓名、性别、年龄&#xff09;要求年龄随机在18-30之间&#xff0c;性别也是随机的 姓名也是随机的首先声明一个带有&#xff08;姓名、性别、年龄&#xff09;属性的类…

作者头像 李华
网站建设 2026/6/25 7:48:35

RouterOS 7.19 x86深度技术解析:网络协议优化与系统稳定性关键技术

技术前沿 【免费下载链接】MikroTikPatch 项目地址: https://gitcode.com/gh_mirrors/mikr/MikroTikPatch MikroTikPatch开源项目持续跟踪RouterOS系统演进&#xff0c;为网络工程师和系统管理员提供深度技术洞察。RouterOS 7.19 x86版本作为重要更新&#xff0c;在网络…

作者头像 李华
网站建设 2026/7/1 7:06:36

你还在盲调量子电路?这3个VSCode插件让你秒变QML调试高手

第一章&#xff1a;量子机器学习的 VSCode 调试在量子机器学习项目中&#xff0c;调试复杂算法和量子线路是开发过程中的关键环节。Visual Studio Code&#xff08;VSCode&#xff09;凭借其强大的扩展生态和集成调试能力&#xff0c;成为该领域的理想开发环境。通过配置 Pytho…

作者头像 李华