当前位置:网站首页>C语言:学生管理系统(链表版)
C语言:学生管理系统(链表版)
2022-08-04 01:06:00 【白的夜gxw】
这是一个小型的管理系统,使用链表进行实现,好了废话不多说,直接上代码,有需要的同学自取。运行的开发环境是vscode.
//学生管理系统(链表版)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student{
int num;//学号
char name[20];//姓名
float score;//分数
struct student *next;
}Node;
int count=0;//记录学生个数
//声明函数
Node * CreateLinkList();//创建链表
void PrintLinkList(Node *header);//打印链表
Node * InsertLinkList(Node *header);//插入链表
Node * DeleteLinkList(Node *header);//删除链表
Node * ChangeLinkList(Node *header);//更改链表
void FreeLinkList(Node *header);//释放链表
void Menu();//菜单
//创建链表
Node * CreateLinkList()
{
Node *header=NULL;
Node *prev,*pcurrent;
prev=pcurrent=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(prev==NULL){
puts("fail");
exit(-1);
}
printf("请输入第%d个学生的学号:",count+1);
scanf("%d",&pcurrent->num);
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pcurrent->name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&pcurrent->score);
while(pcurrent->num)
{
count++;
if(count==1)
{
header=pcurrent;
prev=pcurrent;
}
else
{
prev->next=pcurrent;
prev=pcurrent;
}
pcurrent=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(pcurrent==NULL){
puts("fail");
exit(-1);
}
printf("请输入第%d个学生的学号:",count+1);
scanf("%d",&pcurrent->num);
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pcurrent->name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&pcurrent->score);
}
prev->next=NULL;
return header;
}
//插入链表
Node * InsertLinkList(Node *header)
{
Node *pcurrent,*prev;
Node *target;
prev=pcurrent=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(prev==NULL){
puts("fail");
exit(-1);
}
target=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(target==NULL){
puts("fail");
exit(-1);
}
printf("请输入插入学生的学号:");
scanf("%d",&target->num);
printf("请输入插入学生的姓名:");
scanf("%s",target->name);
printf("请输入插入学生的分数:");
scanf("%f",&target->score);
pcurrent=header;
if(header==NULL)
{
//插入的节点就是头结点
header=target;
target->next=NULL;
}
else
{
//插入的节点是普通节点
//按照分数从小到大排序
while((pcurrent->score < target->score) && pcurrent->next)
{
prev=pcurrent;
pcurrent=pcurrent->next;
}
if(pcurrent->score > target->score)
{
if(pcurrent==header)
{
header=target;
target->next=pcurrent;
}
else
{
prev->next=target;
target->next=pcurrent;
}
}
else
{
pcurrent->next=target;
target->next=NULL;
}
}
count++;
return header;
}
//删除链表
Node * DeleteLinkList(Node *header)
{
Node *pcurrent=header,*prev;
Node *pdel=malloc(sizeof(Node));
printf("请输入删除学生的学号:");
scanf("%d",&pdel->num);
if(header==NULL)
{
printf("这是一个空链表");
return NULL;
}
else
{
while(pcurrent->num!=pdel->num && pcurrent->next)
{
prev=pcurrent;
pcurrent=pcurrent->next;
}
if(pcurrent->num==pdel->num)
{
if(pcurrent==header)
{
header=pcurrent->next;
}
else
{
prev->next=pcurrent->next;
}
}
else
{
printf("没有这个节点!");
}
}
count--;
free(pdel);
return header;
}
//更改链表
Node * ChangeLinkList(Node *header)
{
Node *pcurrent;
int tnum;
float sc;
pcurrent=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(pcurrent==NULL){
puts("fail");
exit(-1);
}
pcurrent=header;
printf("请输入需要更改的学生的序号:");
scanf("%d",&tnum);
if(header==NULL)
{
printf("NULL Linklist\n");
return NULL;
}
else
{
while(tnum!=pcurrent->num && pcurrent->next)
{
pcurrent=pcurrent->next;
}
if(tnum==pcurrent->num)
{
printf("你要更改的分数是;");
scanf("%f",&sc);
pcurrent->score=sc;
}
else
{
printf("没有找到!\n");
}
}
return header;
}
//打印链表
void PrintLinkList(Node *header)
{
Node *pcurrent=header;
if(header==NULL)
{
//空链表
printf("这是一个空链表\n");
}
else
{
printf("\n\n一共打印%d个学生的信息\n",count);
printf("============================================\n");
while(pcurrent)
{
printf("学号:%d\t姓名:%s\t分数:%.0f\n",pcurrent->num,pcurrent->name,pcurrent->score);
pcurrent=pcurrent->next;
}
printf("============================================\n");
}
}
//释放链表
void FreeLinkList(Node *header)
{
Node *pcurrent=(Node *)malloc(sizeof(Node));
//判断申请是否成功
if(pcurrent==NULL){
puts("fail");
exit(-1);
}
pcurrent=header;
while(pcurrent)
{
header=pcurrent->next;
free(pcurrent);
pcurrent=header;
}
}
void Menu()
{
printf("\t\t******************************************************\n");
printf("\t\t* 学 生 管 理 系 统 *\n");
printf("\t\t*\t1.创建学生信息 2.添加学生信息 *\n");
printf("\t\t*\t3.删除学生信息 4.更改学生信息 *\n");
printf("\t\t******************************************************\n");
}
int main()
{
Node *stu;
int num;
do
{
Menu();
printf("请输入:(按5进行退出)");
scanf("%d",&num);
switch (num)
{
case 1://创建链表
stu=CreateLinkList();
PrintLinkList(stu);
break;
case 3: //删除节点
stu=DeleteLinkList(stu);
PrintLinkList(stu);
break;
case 2: //插入节点
stu=InsertLinkList(stu);
PrintLinkList(stu);
break;
case 4://更改链表
stu=ChangeLinkList(stu);
PrintLinkList(stu);
break;
case 5://退出
break;
}
}while(num!=5);
FreeLinkList(stu);
system("pause");
return 0;
}
边栏推荐
猜你喜欢

手撕Gateway源码,今日撕工作流程、负载均衡源码

电子组装行业对MES管理系统的需求分析

互斥锁、读写锁、自旋锁,以及原子操作指令xaddl、cmpxchg的使用场景剖析

动态内存二

Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar

What warehouse management problems can WMS warehouse management system solve in the electronics industry?

nodejs+npm的安装与配置

哎,又跟HR在小群吵了一架!

typescript52 - simplify generic function calls

typescript54 - generic constraints
随机推荐
Is there any jdbc link to Youxuan database documentation and examples?
nodejs installation and environment configuration
Quickly build a website with static files
600MHz频段来了,它会是新的黄金频段吗?
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
【日志框架】
Getting started with MATLAB 3D drawing command plot3
typescript48 - type compatibility between functions
Summary of GNSS Articles
谁说程序员不懂浪漫,表白代码来啦~
Jmeter cross-platform operation CSV files
GeoAO:一种快速的环境光遮蔽方案
boot issue
LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
Linux安装mysql最简单教程(一次成功)
C # WPF equipment monitoring software (classic) - the next
C# WPF设备监控软件(经典)-下篇
教你如何定位不合理的SQL?并优化之
How to find the cause of Fiori Launchpad routing errors by single-step debugging