C语言链表实现简单的学生信息管理系统

C语言链表实现增删查改

#include
#include
#include

typedef struct student{
	char num[20];
	char name[20];
	int Chanese;
	int English;
	int Math; 
	struct student *next;
}Lnode,* LinkList;
void Display(LinkList list){
	Lnode *p = list->next;
	while( p != NULL ){
		printf("链表值为:\n");
		printf("%-16s%-16s%-8d%-8d%-8d\n",p->num,p->name,p->Chanese,p->English,p->Math);
		p = p->next;
	}
}
void InitList(LinkList list){
	Lnode *head=list;
	head = (LinkList)malloc(sizeof(Lnode));
	head->next = NULL;
}

void AddStudent(LinkList list){
	Lnode *p = list,*node;
	node = (LinkList)malloc(sizeof(Lnode));
	while(p->next != NULL){
		p = p->next;
	}
	p->next = node;
	p = node;
	printf("请输入该生的学号,姓名,语文成绩,英语成绩,数学成绩:\n");
	scanf("%s",&node->num);
	scanf("%s",&node->name);
	scanf("%d",&node->Chanese);
	scanf("%d",&node->English);
	scanf("%d",&node->Math);
	p->next = NULL;
}
void Search_name(LinkList list){
	Lnode *p = list->next;
	char a[20];
	printf("请输入要查找学生的姓名:\n");
	scanf("%c",&a);
	while( p != NULL ){
		if(strcmp(a,p->name) == 0){
			printf("查找结果如下:\n学号\t姓名\t语文成绩\t英语成绩\t数学成绩\t");
			printf("%-16s%-8s%-8d%-8d%-8d\n",p->num,p->name,p->Chanese,p->English,p->Math); 
		}
		p = p->next;
	}
}
void Search_num(LinkList list){
	Lnode *p = list->next;
	char a[20];
	printf("请输入要查找学生的学号:\n");
	scanf("%s",&a);//不要忘了去地址符 
	//Display(p);
	//printf("输入的值为:%s",a); 
	while( p != NULL ){
		if(strcmp(a,p->num) == 0){
			printf("查找结果如下:\n学号\t姓名\t语文成绩\t英语成绩\t数学成绩\t\n");
			printf("%-s\t%-s%\t%-d\t%-d\t%-d\t\n",p->num,p->name,p->Chanese,p->English,p->Math); 
		}
		p = p->next;
	}
}
void SearchStudent(LinkList list){
	int n = 0;
	printf("请选择查找方式:\n1.按姓名查找\n2.按学号查找\n");
	scanf("%d",&n);
	printf("输入的值为:%d\n",n);
	if ( n==1 ){
		Search_name(list);
	}
	else if( n==2 ){
		Search_num(list);
	}
	else{
		printf("输入有误!\n");
	}	
}
void Dele_name(LinkList list){
	Lnode *pre = list,*p = pre->next,*q;
	char a[20];
	printf("请输入要删除学生的姓名:\n");
	scanf("%s",&a);
	while( p != NULL ){
		if(strcmp(a,p->name) == 0){
			q = p;						//	q = p;
			p = p->next;				//	pre->next = p->next;
			pre->next = p;				//	p = p->next;
			free(q);					//注意上面的错误 
		}
		else{//这里要加else 
			pre = p;
			p = p->next;	
		}	
	}
}
void Dele_num(LinkList list){
	Lnode *pre = list,*p = pre->next,*q;
	char a[20];
	printf("请输入要删除学生的学号:\n");
	scanf("%s",&a);//要用%s 和 &a  
	while( p != NULL ){
		printf("测试\n");
		if(strcmp(a,p->num) == 0){
		
			q = p;
			p = p->next;
		//	pre->next = p->next; 
		//	p = pre->next;
			pre->next = p;
			free(q);
			printf("测试2\n");
		}
		else{
			pre = p;
			p = p->next;	
		}
		
	}
}
void DeleStudent(LinkList list){
	printf("选择删除方式:\n1.按姓名删除\n2.按学号删除\n");
	int n = 0;
	scanf("%d",&n);
	if ( n==1 ){
		Dele_name(list);
	}
	else if( n==2 ){
		Dele_num(list);
	}
	else{
		printf("输入有误!\n");
	}	
}
void Change_name(LinkList list){
	Lnode *p = list->next;
	char a[20],s[20];
	int n=0;
	printf("请输入要修改学生的姓名:\n");
	scanf("%s",&a);
	while( p != NULL ){
		if(strcmp(a,p->name) == 0){
			printf("选择要修改的学生信息;\n"); 
			printf("1.学号 2.姓名 3.语文成绩 4.英语成绩 5.数学成绩"); 
			scanf("%d",&n);
			switch(n){
			case 1:
				printf("学号修改为:\n");
				scanf("%s",&p->num); break;
			case 2:
				printf("姓名修改为:\n");
				scanf("%s",&p->name);break;
			case 3:
				printf("语文成绩修改为:\n");
				scanf("%d",&p->Chanese);break;
			case 4:
				printf("英语成绩修改为:\n");
				scanf("%d",&p->English);break;
			case 5:
				printf("数学成绩修改为:\n");
				scanf("%d",&p->Math);break;		
			}
			p = p->next;
		}
		else
			p = p->next;
	}
}
void Change_num(LinkList list){
	Lnode *p = list->next;
	char a[20],s[20];
	int n=0;
	printf("请输入要修改学生的学号:\n");
	scanf("%s",&a);
	while( p != NULL ){
		if(strcmp(a,p->num) == 0){
			printf("选择要修改的学生信息;\n"); 
			printf("1.学号 2.姓名 3.语文成绩 4.英语成绩 5.数学成绩"); 
			scanf("%d",&n);
			switch(n){
			case 1:
				printf("学号修改为:\n");
				scanf("%s",&p->num); break;
			case 2:
				printf("姓名修改为:\n");
				scanf("%s",&p->name);break;
			case 3:
				printf("语文成绩修改为:\n");
				scanf("%d",&p->Chanese);break;
			case 4:
				printf("英语成绩修改为:\n");
				scanf("%d",&p->English);break;
			case 5:
				printf("数学成绩修改为:\n");
				scanf("%d",&p->Math);break;
			}
			p = p->next;
		}
		else
			p = p->next;
	}
}
void ChangeStudent(LinkList list){
	printf("选择修改方式:\n1.查姓名修改\n2.查学号修改\n");
	int n = 0;
	scanf("%d",&n);
	if ( n==1 ){
		Change_name(list);
	}
	else if( n==2 ){
		Change_num(list);
	}
	else{
		printf("输入有误!\n");
	}
}

int Average(LinkList list){
	Lnode *p = list->next;
	int count = 0, aver = 0, sum = 0;
	int n;
	printf("选择哪一门的平均成绩:\n1.语文 2.英语 3.数学 \n");
	scanf("%d",&n);
	switch (n){
		case 1:
			#define  subject p->Chanese
			break; 
		case 2:
			#define  subject p->English
			break; 
		case 3:
			#define  subject p->Math
			break; 		
	}
	while( p != NULL ){
		count++;
		printf("%d次的值为%d\n",count,subject);
		sum += subject;
		printf("sum值为:%d\n",sum);
		p = p->next;
	}
	return aver = sum/count;
}


int main(){
	LinkList list;
	int aver=0;
	AddStudent(list);
//	SearchStudent(list);
//	AddStudent(list);
//	DeleStudent(list);
//	Display(list);
//	ChangeStudent(list);
//	Display(list);
//	AddStudent(list);
	AddStudent(list);
	aver = Average(list);
	printf("平均值为:%d\n",aver);
} 

你可能感兴趣的:(C语言)