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; 
		}
} 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竹等寒

谢过道友支持,在下就却之不恭了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值