学生信息管理系统

#include"stdio.h"
#include"stdlib.h"

//专用于链表的结构
struct student_info
{
	int stu_id;
	char stu_name;
	char stu_sex;
	short age;
	struct student_info *next;
};
struct student_info *p_head;

//--------------辅助函数---------------
//供用户选择的操作
void print_item();
//获取用户选择的操作,并作为函数值返回
int get_choose();
//统计链表中结点的个数,参数是链表的头指针
int sum_node(struct student_info *p_head);

//--------------用于实现用户请求的函数----------------
//查看所有学生信息,参数是链表头指针
void print_student_info(struct student_info *p_head);
//插入结点到链表,参数是链表头指针和结点的插入位置
void insert_student_info(struct student_info *p_head,int pos);
//删除链表中的结点,参数是链表头指针和要删除的结点位置
void del_student_info(struct student_info *p_head,int pos);
//查找链表中的结点,参数是链表头指针和学生的姓名
void search_student_info(struct student_info *p_head,char stu_name);

int main()
{
	puts("\t学生信息系统--");
	p_head=(struct student_info *)malloc(sizeof(struct student_info));
	p_head->next=NULL;
	print_item();
	printf("\n");
	int choose=0;
	int pos=0;
	choose=get_choose();
	while(choose>=1&&choose<=6)
	{
		switch(choose)
		{
			case 1:
				print_student_info(p_head);
				printf("\n");
				break;
			case 2:
				puts("插入信息到那个位置");
				scanf("%d",&pos);
				insert_student_info(p_head,pos);
				printf("\n");
				break;
			case 3:
				puts("删除哪一个位置的结点?");
				scanf("%d",&pos);
				del_student_info(p_head,pos);
				printf("\n");
				break;
			case 4:
				puts("输入要查找学生姓名");
				char c;
				getchar();
				scanf("%c",&c);
				search_student_info(p_head,pos);
				printf("\n");
				break;
		}
		if(choose==6)
		{
				puts("--退出程序--");
				printf("\n");
				break;
	    }
		choose=get_choose();
	}
	printf("\n");
	return 0;
}

void print_item()
{
	puts("1.查看所有学生信息");
	puts("2,插入学生信息");
	puts("3.删除学生信息");
	puts("4.查找学生信息");
	puts("6.退出系统");
}
int get_choose()
{
	int choose_item=0;
	printf("您输入的选择是:");
	scanf("%d",&choose_item);
	if(choose_item<1&&choose_item>6)
	{
		puts("输入有误,请重新输入");
	}
	else
	{
		return choose_item;
	}
}

int sum_node(struct student_info *p_head)
{
	int count=0;
	struct student_info *p;
	p=p_head->next ;
	while(p)
	{
		count++;
		p=p->next;
	}
	return count;
}

void print_student_info(struct student_info *p_head)
{
	struct student_info *p;
	int count=0;
	p=p_head->next;
	while(p)
	{
		if(count==0)
		{
			puts("学号\t姓名\t性别\t年龄");
		}
		printf("%d\t%c\t%c\t%hd\n",p->stu_id,p->stu_name,p->stu_sex,p->age);
		count++;
		p=p->next;
	}
	if(count==0)
	{
		puts("--当前没有学生信息--");
		return;
	}
	puts("--以上是全部学生信息--");
}

void insert_student_info(struct student_info *p_head,int pos)
{
	struct student_info *p_new;
	p_new=(struct student_info *)malloc(sizeof(struct student_info));
	//1--获取用户输入的数据
	if(p_new!=NULL)
	{
		puts("请依次输入:学号,姓名,性别,年龄。(中间使用一个空格分隔)");
		scanf("%d %c %c %d",&p_new->stu_id,&p_new->stu_name,&p_new->stu_sex,&p_new->age);
	}
	else
	{
		puts("--插入数据失败--");
	}
	//插入结点到链表
	int count_node;
	count_node=sum_node(p_head);
	int min;
	if(count_node<pos)
		min=count_node;
	else
		min=pos-1;
	struct student_info *p_front;
	p_front=p_head;
	for(int i=1;i<=min;i++)
	{
		p_front=p_front->next;
	}
	p_new->next=p_front->next;
	p_front->next=p_new;
	puts("--插入数据成功--");
}

void del_student_info(struct student_info *p_head,int pos)
{
	int count_node;
	count_node=sum_node(p_head);
	if(count_node<pos)
	{
		puts("--当前位置没有学生信息--");
	}
	else
	{
		struct student_info *p_front,*p_del;
		p_front=p_head;
		for(int i=1;i<=pos-1;i++)
		{
			p_front=p_front->next;
		}
		p_del=p_front->next;
		p_front->next=p_del->next;
		free(p_del);
		puts("--删除成功--");
	}
}

void search_student_info(struct student_info *p_head,char stu_name)
{
	struct student_info *p;
	p=p_head;
	int count_node;
	count_node=sum_node(p_head);
	int i=0;
	for(i=1;i<=count_node;i++)
	{
		p=p->next;
		if(p->stu_name==stu_name)
		{
			puts("学号\t姓名\t性别\t年龄");
			printf("%d\t%c\t%c\t%hd\n",p->stu_id,p->stu_name,p->stu_sex,p->age);
			puts("--以上是找到的学生信息--");
			return;
		}
	}
	if(i==count_node+1)
	{
		puts("--没有找到这个学生的信息--");
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值