2021-05-23

C语言学生管理系统链表实现(个人学习记录)

#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include"string.h"
#define LEN sizeof (struct stu)
#define ctu struct stu
struct stu   //这里使用stu命名
{
	int num=0;
	char name[10];
	int score=0;
	struct stu *next;
 };
 
ctu*create_list()
{
	ctu*headnode=(ctu*)malloc(LEN);
	headnode->next=NULL;
	if(headnode->next==NULL)
	{
		printf("链表初始化成功!\n");
	}
	else printf("链表初始化失败!\n");
	return headnode;
}

ctu* create_node(ctu*headnode)
{
	ctu*o=headnode;
	ctu*p=(ctu*)malloc(LEN);
	p->next=NULL;
	printf("输入学号:");
	scanf("%d",&p->num);
	printf("输入姓名:");
	scanf("%s",&p->name);
	printf("输入成绩:");
	scanf("%d",&p->score);
	if(headnode==NULL)
	{
		headnode=p;
		p->next=NULL;
	}
	else
	{
		while(o->next!=NULL)
		{
			o=o->next;
		}
		o->next=p;
	}
	return headnode;
}

void print_list(ctu*headnode)
{
	ctu*s=headnode;
	if(s==NULL)
	printf("此链表为空!\n");
	else if(s->next)
	{
		printf("学号\t名字\t分数\n");
		while(1)
		{
		s=s->next;
		printf("%d\t%s\t%d\n",s->num,s->name,s->score);
		if(s->next==NULL)break;
		}
	}	
}

void name_find(ctu*headnode)
{
	char s[10];
	ctu*p=headnode;
	printf("输入要查找的学生名字:");
	scanf("%s",&s);
	p=p->next;
	while(1)
	{
		if(strcmp(s,p->name)==0)
		{
			printf("该学生的学号为:%d\n",p->num);
			printf("该学生的成绩为:%d\n",p->score);
			break;
		}
		else if(p==NULL)
		{
			printf("系统没有此学生!\n");
			break;
		}
		p=p->next;
	}
}

void index_find(ctu*headnode)
{
	int n=0;
	ctu*p=headnode;
	printf("输入需要查找的位置:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		p=p->next;
	}
	printf("该位置的学生信息为:\n学号\t名字\t分数\n");
	printf("%d\t%s\t%d\n",p->num,p->name,p->score);
 } 
 
void index_inset(ctu*headnode)
{
	int n=0;
	ctu*p=headnode;
	ctu*o=(ctu*)malloc(LEN);
	o->next=NULL;
	for(int i=0;i<n;i++)
	{
		p=p->next;
	}
	printf("输入学号:");
	scanf("%d",&o->num);
	printf("输入姓名:");
	scanf("%s",&o->name);
	printf("输入成绩:");
	scanf("%d",&o->score);
	o->next=p->next;
	p->next=o;
	printf("学生信息插入成功!\n");
}

void name_delete(ctu*headnode)
{
	int n=0;
	ctu*p=headnode;
	printf("输入需要被删除信息的位置:");
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		p=p->next;
	}
	p->next=p->next->next;
	printf("信息删除成功!\n");
 } 
 
int count_print(ctu*headnode)
{
	int count=0;
	ctu*p=headnode;
	while(p->next)
	{
		count+=1;
		p=p->next;
	}
	return count;
}

void select_print()
{
	printf("********************\n");
	printf("**学生成绩管理系统**\n");
	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");
}

int main()
{
	int n;
	ctu*list;
	select_print();
	while(1)
	{
		printf("请选择操作(0~8):");
		scanf("%d",&n);
		switch(n)
		{
			case 0:
				{
					exit(0); 
					break;
				}
			case 1:
				{
					list=create_list();
					break;
				}
			case 2:
				{
					int t;
					printf("输入学生成绩的个数:");
					scanf("%d",&t); 
					for(int i=0;i<t;i++)
					create_node(list);
					break;
				}
			case 3:
				{
				print_list(list);
				break;
				}
			case 4:
				{
					name_find(list);
					break;
				}
			case 5:
				{
					index_find(list);
					break;
				}
			case 6:
				{
					int t=count_print(list);
					printf("输入需要插入的位置以及学生信息:");
					scanf("%d",&n);
					if(t<n)
					{
						create_node(list);
					}
					else
					index_inset(list);
					break;
				}
			case 7:
			{
				name_delete(list);
				break;
			 } 
			case 8:
				{
					int i=0;
					i =count_print(list);
					printf("学生统计个数为%d\n",i);
					break;
				}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值