链表实现简易的学生管理系统(C语言版本)

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
	char name[20];
	char num[20];
	int age;
	char sex[10];
	int score;

}student;

typedef struct Lnode{
	student s;
	struct Lnode *next;

}sclass,*psclass;
//创建链表使用的是带头结点的头插法
void create(psclass head){
	int n;
	psclass p;
	//尾结点
	printf("请输入要添加的学生人数:\n");
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		p=(psclass)malloc(sizeof(sclass));//新节点
		printf("请输入要插入的第%d个学生\n",i+1);
		printf("请输入要插入的学生的学号:");
		scanf("%s",p->s.num);
		printf("请输入要插入的学生的姓名:");
		scanf("%s",p->s.name);
		printf("请输入要插入的学生的性别:");
		scanf("%s",p->s.sex);
		printf("请输入要插入的学生的年龄:");
		scanf("%d",&(p->s.age));
		printf("请输入要插入的学生的成绩:");
		scanf("%d",&(p->s.score));

		p->next=head->next;
		head->next=p;

	
	}
	//free(p);

	
}
//打印链表
void printL(psclass head){
	psclass p;
	p=head;
	if(p->next==NULL){
	
		printf("学生管理系统为空!\n");
		
	}
	else{
		printf("学生信息:\n");
		printf("学号\t\t姓名\t\t性别\t\t年龄\t\t成绩\t\n");
		while(p->next!=NULL){
			p=p->next;	
			printf("%s\t",p->s.num);
			printf("\t%s\t",p->s.name);
			printf("\t%s\t",p->s.sex);
			printf("\t%d\t",(p->s.age));
			printf("\t%d\t\n",(p->s.score));

		}
	
	}
	
}
//查找某一学生的信息
void search(psclass head){
	psclass p;
	p=head->next;
	psclass s;
	s=(psclass)malloc(sizeof(sclass));//新节点
	int flag=0;
	char name[20]={0};
	if(p==NULL){
	
		printf("学生管理系统为空!\n");
		
	}
	else
	{
		printf("请输入要查找的学生的姓名:\n");
		scanf("%s",name);
		while(p!=NULL){
			if(strcmp(p->s.name,name)==0){
				s=p;
				flag=1;
				break;
			}
			else{
			
				p=p->next;
			}
		
		}	
		if(flag==1){
			printf("这个学生的学号是: %s\n",s->s.num);
			printf("这个学生的性别是: %s\n",s->s.sex);
			printf("这个学生的年龄是: %d\n",(s->s.age));
			printf("这个学生的成绩是: %d\n",(s->s.score));
		
		
		}
		else if(flag==0)
			printf("这个学生不存在!\n");


	}
//	free(s); 这里不能用free,这个问题卡了很久,这里free的话,查找到的节点就被销毁了

}
//删除某一学生的信息
void delete(psclass head){
	psclass p;
	p=head;

	int flag=0;
	char name[20]={0};
	if(p->next==NULL){
	
		printf("学生管理系统为空!\n");
		
	}
	else
	{
		printf("请输入要删除的学生的姓名:\n");
		scanf("%s",name);
		
		while(p->next!=NULL){
//				s=p;
				if(strcmp(p->next->s.name,name)==0){
					flag=1;
					break;
				}
				else{
				
					p=p->next;
				}
		
		}	
		if(flag==1){	
			p->next=p->next->next;
		
			
			printf("删除成功!");
		}
		else if(flag==0)
			printf("这个学生不存在!\n");


	}


}




void menu(){

		printf("********************************************************\n");
		printf("                       学生管理系统                     \n");
		printf("1.创建学生信息\n");
		printf("2.查看所有学生信息\n");
		printf("3.查找学生信息\n");
		printf("4.删除学生信息\n");
		printf("5.退出学生管理系统\n");
		printf("********************************************************\n");




}



int main(){

	psclass head;
	int cmd;
	head=(psclass)malloc(sizeof(sclass));


	//head=NULL;
	head->next=NULL;
	while(1){
		menu();
		printf("请输入要进行的操作:");
		scanf("%d",&cmd);
		switch(cmd){
			case 1:
				create(head);
				break;
			case 2:
				printL(head);
				break;
			case 3:
				search(head);
				break;
			case 4:
				delete(head);
				break;
			case 5:
				exit(-1);
			default:
				printf("请输入正确的操作!\n ");
				break;	
		
		}
	}
	free(head);



	return 0;
}
	




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值