C语言:单链表删除学生信息,增加学生信息(简易版)
假设用户都是正常的,不会输入一些乱七八糟的东西。
功能1:输出学生学号和成绩,用动态连链表来存放,继续存放学生信息的时候可以继续输入之前输入过的学号信息,打印的时候会分开打印(因为是简易版本,没有太完善)
功能2 :删除信息,用户输入要删除的学号的时候,如果这个学号在之前录入信息的时候录入了多次,比如说有两个学号都是2,那么在你输入要删除2这个学号的时候,会把所有学号为2的学生信息删除掉。
叨叨几句:因为找不到很好的办法去删除有多个相同学号的办法,所以我采用了每次删除了一个之后,要重新从单链表的头冲洗再寻找一次还有没有这个学号,如果有大佬知道更好的办法可以私信我哈~
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define PF(format, ...) printf(#format,##__VA_ARGS__)
typedef struct Add_Student{//存放学生学号和成绩单链表 的单链表
long long stu_num;
int grade;
struct Add_Student *stu_Infor_next;
}student_InF;
student_InF *delet_Room = NULL;//待删除的信息空间
void allocate_student(student_InF **);//分配学生存放信息空间
void PT(student_InF *student_Information);
bool jude_put();//判断是否继续录入
void delete_infor(student_InF **student_Information);//删除信息
bool jude_delete(); //判断是否继续删除
int main(void)
{
student_InF *student_Information = NULL;//学生信息
while(1)
{
allocate_student(&student_Information);
if(!jude_put()) break;//判断是否继续录入
}
PT(student_Information);
while(1)
{
delete_infor(&student_Information);//删除信息
PT(student_Information);
if(!jude_delete()) break;//判断是否继续删除信息
}
return 0;
}
void allocate_student(student_InF **student_Information)//分配学生存放信息空间
{
student_InF *temp = NULL, *new_student_Infor;
temp = *student_Information;
new_student_Infor = (student_InF*)malloc(sizeof(student_InF));//动态分配内存空间
PF(请输入学生学号:);
scanf("%llu", &new_student_Infor->stu_num);
getchar();
PF(请输入学生成绩:);
scanf("%d", &new_student_Infor->grade);
getchar();
if(new_student_Infor == NULL)
{
PF(内存分配失败!);
exit(1);
}
if(*student_Information == NULL)
{
*student_Information = new_student_Infor;
new_student_Infor->stu_Infor_next = NULL;//新空间的节点直接指向NULL
}
else
{
while(temp->stu_Infor_next != NULL)//找到信息尾部
{
temp = temp->stu_Infor_next;
}
temp->stu_Infor_next = new_student_Infor;
new_student_Infor->stu_Infor_next = NULL;
}
}
void PT(student_InF *student_Information)
{
student_InF *temp;
temp = student_Information;
PF(学生信息如下:\n);
if(student_Information == NULL)
{
PF((空白)\n);
return;
}
while(1)
{
if(temp != NULL)
{
PF(学号:%llu 成绩:%d\n, temp->stu_num, temp->grade);
temp = temp->stu_Infor_next;//指向下一个待输出空间
}
else break;
}
}
void delete_infor(student_InF **student_Information)//删除信息
{
long long num;
student_InF *current = *student_Information;//表示当前的在单链表标记的位置
student_InF *previous = NULL;//表示current前一个位置
bool jude = false; //判断用户输入的学号是否存在
PF(请输入你要删除的学号:);
scanf("%llu", &num);
getchar();//接收回车键
while(1)
{
if(current == NULL) //判断是否到尾部了
{
if(jude == false)
{
PF(不存在该学生学号!\n);
}
break;
}
if(current != NULL && current->stu_num == num)
{
jude = true;//证明输入的学号存在
if(previous == NULL)//表示要删除的是第一个
{
(*student_Information) = (*student_Information)->stu_Infor_next;//直接把头改掉,因为要删除的是第一个学号
free(current);//释放要删除的信息
}
else
{
previous->stu_Infor_next = current->stu_Infor_next;//前一个的next指向当前的next ,当前的next也就是当前下一个信息区
free(current);//释放当前的信息区
}
current = *student_Information;//出去从头再次扫描一遍 看看是否还有重复的学号
}
previous = current;
if(current != NULL) current = current->stu_Infor_next;//标记位置不断往前推进 ,先判断是否到结尾了,没到结尾才能让位置向前
}
}
bool jude_delete()//判断是否继续删除
{
char ch;
PF(是否继续删除信息(Y/N):);
ch = getchar();
if(ch == 'N') return false;
else if(ch == 'Y')
{
return true;
}
}
bool jude_put()//判断是否继续录入
{
char ch;
PF(是否继续录入(Y/N):);
ch = getchar();
if(ch == 'N') return false;
else if(ch == 'Y')
{
return true;
}
}
本文来自博客园,作者:竹等寒,转载请注明原文链接。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析