学生信息管理系统 C语言 链表存储、遍历、文件读写

学生信息管理系统的功能有,也可以自己增加或者改进一些函数功能。
 ,,
在main函数里调用这8个函数
学生信息包含姓名、年龄、学号、成绩,需要定义一个结构体(结构体是全局变量,所以需要全局声明):

typedef struct _student{
	char name[20];
	int age;
	int stuNum;
	int score;
}student;

需要有一个存储数据的空间,所以使用单链表存储,定义如下:

typedef struct _Node{
	student stu1;
	struct _Node* pNext;
}Node;

此时需要给链表一个头:

Node *g_head=NULL;

录入学生信息:
1)创建一个新节点,结点里包括结构体数据,和下一个指针。
2)需要判断头结点是不是空的,如果是空的,则此时需要将新节点付给头结点,如果不是空的,那么该结点的下一个指针=头结点(头插法,能用就行)。
3)然后就输入数据scanf("%s",&p->stu1.age);
4)最后提示一下该学生信息输入完毕!
5)现在只输入了一个学生信息,此时只需要在主函数里写一个循环就可以无限调用该函数进行数据录入。

void input(){
	printf("请输入学生信息\t\n");
	Node *pNewNode=(Node*)malloc(sizeof(Node));//创建一个新节点。
	pNewNode->pNext=NULL;
	
	if(g_head==NULL){
		g_head=pNewNode;
	}
	else{
		pNewNode->pNext=g_head;
		g_head=pNewNode;
	}
	printf("请输入姓名: ");
	scanf("%s",pNewNode->stu1.name);
	printf("请输入年龄: ");
	scanf("%d",&pNewNode->stu1.age);
	printf("请输入学号 : ");
	scanf("%d",&pNewNode->stu1.stuNum);
	printf("请输入成绩 : ");
	scanf("%d",&pNewNode->stu1.score);
printf("该学生信息输入完毕!\n\n");
}

查看信息:
1)需要对链表进行遍历然后printf;
2)新建一个结点指针然后 Node* p=g_head; while(p!=NULL){ printf p=p->next}(注:以上代码只是功能的描述)
代码:

void printdate(){
	Node* p=g_head;
	printf("\t姓名\t年龄\t学号\t成绩\n");
	while(p!=NULL)
	{
		printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
		p=p->pNext;
	}
}

保存信息:
1)先用文件指针指向一个要使用的文件,
2)便利所有链表的数据,将遍历的数据写入文件指针指向的文件里

void save(){
	FILE *fp=fopen("E:\\stu.data","w");
	if(fp==NULL){
		printf("文件打开失败");
		return;
	}
	Node* p=g_head;
	while(p!=NULL){
		fwrite(&p->stu1,1,sizeof(student),fp);
		p=p->pNext;
	}
	fclose(fp);
	printf("文件保存成功!\n");
}

文件读取
1)先用文件指针指向一个使用的文件,
2)这里需要开辟链表来将读取到的数据写入链表里的数据里的结构体里,
3)需要判断一下是否读取到了数据,将文件里的数据先读到结构体数组里
4)然后while循环里 ,,将结构体数组里的数据复制给链表,
5)需要将每一个节点排好队列,这里用头插法,每复制一个就会开辟一个结点,
代码:

void rs(){
	FILE* fp=fopen("E:\\stu.data","r");
	if(fp==NULL){
		printf("文件打开失败");
	}
	printf("文件读取成功!\n 查看文件请按2\n");
	student stu;
	
	while(fread(&stu,1,sizeof(student),fp)){
		Node* pNewNode=(Node*)malloc(sizeof(Node));
		pNewNode->pNext=NULL;

		memcpy(pNewNode,&stu,sizeof(student));

		if(g_head==NULL){
			g_head=pNewNode;

		}
		else{
			pNewNode->pNext=g_head;
			g_head=pNewNode;

		}
	}
	}

统计人数:
1)遍历链表的时候定义一个整型数组自加最后输出

void count(){
	int a=0;
	FILE* fp=fopen("E:\\stu.data","r");
	if(fp==NULL){
		printf("文件打开失败");
		return;
	}
	Node* p=g_head;
	while(p!=NULL){
		p=p->pNext;
		a++;
	}
	printf("总人数%d",a);
}

查找学生:
1)定义整型数据,输入学号,先遍历再if判断输入的学号和p->stu1.num是否相等,相等的话输出:

void find(){
	int num;
	printf("请输入要查找的学生学号: \n");
	scanf("%d",&num);
	Node* p=g_head;
	while(p!=NULL){
		if(p->stu1.stuNum==num){
			printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
		}
		p=p->pNext;
	}
printf("无该学生信息");
}

修改信息:
1)定义整型数据,输入学号,先遍历再if判断输入的学号和p->stu1.num是否相等,相等的话重新输入:

void change(){
	int num;
	printf("请输入要修改的学生的学号: ");
	scanf("%d",&num);
	Node* p=g_head;
	while(p!=NULL){

		if(p->stu1.stuNum==num){
			printf("请输入姓名: \n");
			scanf("%s",p->stu1.name);
			printf("请输入年龄: \n");
			scanf("%d",&p->stu1.age);
			printf("请输入学号: \n");
			scanf("%d",&p->stu1.stuNum);
			printf("请输入成绩: \n");
			scanf("%d",&p->stu1.score);
			printf("信息更改完毕!");
		}

		p=p->pNext;
	}
	if(p==NULL){
		printf("该学生不存在!\n");
	}

}

删除信息:
1)思路是先遍历,找到了free就可以了,
2)需要定义两个节点指针,如果找到了是头结点直接将头结点付给定义的节点,然后free,
如果不是头结点,将该节点付给定义的节点,p->next=p->next->next;
然后free

void del(){
	int num;

	printf("请输入要删除的学号");
	scanf("%d",&num);
	Node* p=g_head;
	Node*p1,*p2;
	if(p->stu1.stuNum==num){
		p1=p->pNext;

		free(p1);

	}
	if(p->pNext!=NULL){
		p2=p->pNext;
		p->pNext=p->pNext->pNext;
		free(p2);
	}
printf("学号为%d的信息删除成功!\n",num);

}

下面是完整代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _student{
	char name[20];
	int age;
	int stuNum;
	int score;
}student;

typedef struct _Node{
	student stu1;
	struct _Node* pNext;
}Node;

Node *g_head=NULL;
void menu(){
	printf("------------------\n");
	printf("- 1录入信息-\n");
	printf("- 2查看信息-\n");
	printf("- 3保存信息-\n");
	printf("- 4读取信息-\n");	
	printf("- 5统计人数-\n");
	printf("- 6查找信息-\n");
	printf("- 7修改信息-\n");
	printf("- 8删除信息-\n");
	printf("- 0退出-\n");
	printf("退出不要直接点叉,请按0退出!\n查看文件之前请先读取文件!\n");
}
void input(){
	printf("请输入学生信息\t\n");
	Node *pNewNode=(Node*)malloc(sizeof(Node));//创建一个新节点。
	pNewNode->pNext=NULL;
	
	if(g_head==NULL){
		g_head=pNewNode;
	}
	else{
		pNewNode->pNext=g_head;
		g_head=pNewNode;
	}
	printf("请输入姓名: ");
	scanf("%s",pNewNode->stu1.name);
	printf("请输入年龄: ");
	scanf("%d",&pNewNode->stu1.age);
	printf("请输入学号 : ");
	scanf("%d",&pNewNode->stu1.stuNum);
	printf("请输入成绩 : ");
	scanf("%d",&pNewNode->stu1.score);
printf("该学生信息输入完毕!\n\n");
}

void printdate(){
	Node* p=g_head;
	printf("\t姓名\t年龄\t学号\t成绩\n");
	while(p!=NULL)
	{
		printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
		p=p->pNext;
	}
}


void save(){
	
	FILE *fp=fopen("E:\\stu.data","w");
	if(fp==NULL){
		printf("文件打开失败");
		return;
	}
	Node* p=g_head;
	while(p!=NULL){
		fwrite(&p->stu1,1,sizeof(student),fp);
		p=p->pNext;

	}
	fclose(fp);
	printf("文件保存成功!\n");
}


void rs(){
	FILE* fp=fopen("E:\\stu.data","r");
	if(fp==NULL){
		printf("文件打开失败");
	}
	printf("文件读取成功!\n 查看文件请按2\n");
	student stu;
	
	while(fread(&stu,1,sizeof(student),fp)){
		Node* pNewNode=(Node*)malloc(sizeof(Node));
		pNewNode->pNext=NULL;

		memcpy(pNewNode,&stu,sizeof(student));

		if(g_head==NULL){
			g_head=pNewNode;

		}
		else{
			pNewNode->pNext=g_head;
			g_head=pNewNode;

		}
	}
	}



void count(){
	int a=0;
	FILE* fp=fopen("E:\\stu.data","r");
	if(fp==NULL){
		printf("文件打开失败");
		return;
	}
	Node* p=g_head;
	while(p!=NULL){
		p=p->pNext;
		a++;
	}
	printf("总人数%d",a);
}




void find(){
	int num;
	printf("请输入要查找的学生学号: \n");
	scanf("%d",&num);
	Node* p=g_head;
	while(p!=NULL){
		if(p->stu1.stuNum==num){
			printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
		}
		p=p->pNext;
	}
printf("have not");
}



void change(){
	int num;
	printf("请输入要修改的学生的学号: ");
	scanf("%d",&num);
	Node* p=g_head;
	while(p!=NULL){

		if(p->stu1.stuNum==num){
			printf("请输入姓名: \n");
			scanf("%s",p->stu1.name);
			printf("请输入年龄: \n");
			scanf("%d",&p->stu1.age);
			printf("请输入学号: \n");
			scanf("%d",&p->stu1.stuNum);
			printf("请输入成绩: \n");
			scanf("%d",&p->stu1.score);
			printf("信息更改完毕!");
		}

		p=p->pNext;
	}
	if(p==NULL){
		printf("该学生不存在!\n");
	}

}



void del(){
	int num;

	printf("请输入要删除的学号");
	scanf("%d",&num);
	Node* p=g_head;
	Node*p1,*p2;
	if(p->stu1.stuNum==num){
		p1=p->pNext;

		free(p1);

	}
	if(p->pNext!=NULL){
		p2=p->pNext;
		p->pNext=p->pNext->pNext;
		free(p2);
	}
printf("学号为%d的信息删除成功!\n",num);

}



int main()
{
	menu();
	while(1)
	{
		char ch=getch();
		switch(ch){
		case '1':input();break;
		case '2':printdate();break;
		case '3':save();break;
		case '4':rs();break;


		case '5':count();break;
		case '6':find();break;
		case '7':change();break;
		case '8':del();break;



		case '0':exit(0);
		
		}
	}

return 0;
}

喜欢点赞、评论加收藏哦~!

  • 21
    点赞
  • 248
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
数据结构课程设计主要是围绕学生信息管理系统展开,在C语言中使用链表实现文件存储和管理。首先,需要设计学生信息的数据结构,包括学生的姓名、学号、年龄、性别等基本信息,并在链表中进行存储。在链表的设计中,可以使用单向链表或双向链表存储学生信息,每个节点代表一个学生,节点中存储该学生的各项信息。 其次,需要设计各种功能来实现学生信息的管理,例如添加学生信息、删除学生信息、修改学生信息、查询学生信息等操作。这些功能需要通过各种算法来实现,例如遍历链表、插入节点、删除节点等。 另外,在C语言中使用文件来进行数据存储和读取。因此,需要设计文件的读写功能,将链表中的学生信息存储文件中,或者从文件中读取学生信息到链表中。这涉及到文件操作的知识,包括文件的打开、关闭、写入、读取等操作。 除此之外,还需要考虑对学生信息的排序功能,可以按照学号、姓名、年龄等排序来展示学生信息。这需要设计相应的排序算法来实现。 综上所述,数据结构课程设计学生信息管理C语言链表文件主要包括学生信息的数据结构设计、链表实现文件的读写功能设计、各种操作功能的实现以及排序算法的设计。通过这样一个综合的设计,可以帮助学生深入理解数据结构的应用与实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值