链表实现学生系统写入和查询

链表实现学生系统写入和查询,第1张

#include 
#include 
#include 

struct Student* CreateList();
void InsertElement(struct Student*List);
void Search_Stu(struct Student* HEAD, char name[10], long long int ID);

struct Student
{
	char name[10];
	long long int Student_ID;
	int grade;
	int level;
	struct Student* last;
	struct Student* next;

};


int main()
{
	char ch;
	struct Student* Head_List = CreateList();
	printf("****************************\n");
	printf("*                          *\n");
	printf("*         欢迎访问         *\n");
	printf("*                          *\n");
	printf("****************************\n");
	printf("请根据提示完成\n");
	printf("选择模式(添加学生+)(移除学生-)(搜索学生s)\n");
	while ((ch=getchar())!=0)
	{	
		if (ch == '+')
		{
			InsertElement(Head_List);
		}
		else if (ch == '-')
		{
			
		}
		else if (ch == 's')
		{
			char name[10];long long int Student_ID;
			int tmp;
			printf("请选择姓名查询OR学号查询?(1/2)\n");
			scanf("%d", &tmp);
			if (tmp == 1)
			{
				printf("请输入姓名:\n");
				scanf("%s", name);
				Search_Stu(Head_List,name,0);
			}
			else if (tmp == 2)
			{
				printf("请输入学号:\n");
				scanf("%lld", &Student_ID);
				Search_Stu(Head_List, " ", Student_ID);
			}
			
		}
	}

	free(Head_List);

	return 0;
}

struct Student* CreateList()
{
	struct Student* List=(struct Student*)malloc(sizeof(struct Student));
	List->next = NULL;
	return List;
}

void InsertElement(struct Student* Head)
{
	while (Head->next)
	{
		Head = Head->next;
	}
	struct Student* Body_List = (struct Student*)malloc(sizeof(struct Student));
	printf("input(name、id、grade、level):");
	scanf("%s%lld%d%d", Head->name, &(Head->Student_ID), &(Head->grade), &(Head->level));
	Head->last = NULL;
	Head->next = Body_List;
	Body_List->last = Head;
	Body_List->next = NULL;
	Head = Body_List;
	

}
void Search_Stu(struct Student* HEAD, char name[10], long long int ID)
{
	while (HEAD)
	{
		if (strcmp(HEAD->name, name) == 0 || HEAD->Student_ID == ID)
		{
			printf("name:%20s  id:%15lld  grade:%5d  level:%5d \n", HEAD->name, HEAD->Student_ID, HEAD->grade, HEAD->level);
			
		}
		HEAD = HEAD->next;
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1325127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存