C语言链表实现学生管理系统

C语言链表实现学生管理系统
在这里插入图片描述

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

struct Student
{
	unsigned long	ID;
	char			Name[21];
	float			Score;
};

struct Node
{
	struct Student	data;
	struct Node *	next;
};

int Count=0;
struct Node * H=NULL;

struct Node * Insert(struct Node * Head, struct Node * pNode)
{
	struct Node * p=Head,*q=Head;

	if (p==NULL)
	{
		Head=pNode;
		
	}
	else 
	{
		while(p)
		{
			if (pNode->data.Score < p->data.Score)
			{
				q=p;
				p=p->next;
			}
			else
			{
				if (p==Head)
				{
					Head=pNode;
					pNode->next=p;

				}
				else
				{
					q->next=pNode;
					pNode->next=p;
				    
				}
				break;
			}
		}
		if (p==NULL)
		{
			q->next=pNode;
		}
	}
	return Head;
}

void Display(struct Node * Head)
{
	if (Head==NULL)
	{
		printf("当前没有学生成绩信息!\n");
		return;
	}

	struct Node * p=Head;
	for (int i=0;i<Count;i++)
	{
		printf("学号:%lu\t姓名:%s\t成绩:%.1f\n",p->data.ID,p->data.Name,p->data.Score);
		p=p->next;
	}
}

void Add() 
{
	unsigned long ID;
	char Name[21];
	float Score;
	struct Node * Head=NULL;

	printf("请输入学号:");
	scanf("%lu",&ID);

	while (ID)
	{
		printf("请输入姓名:");
		scanf("%s",Name);
		printf("请输入成绩:");
		scanf("%f",&Score);

		struct Node * pNew=(struct Node *) malloc(sizeof (struct Node));
		pNew->data.ID=ID;
		strcpy(pNew->data.Name,Name);
		pNew->data.Score=Score;
		pNew->next=NULL;

		Count++;

		Head=Insert(Head,pNew);

		printf("请输入学号:");
		scanf("%lu",&ID);
	}

	H=Head;
}


void Search(struct Node * Head)
{
	char Name[21];
	int i=0;
	struct Node * p=Head;
	if (p==NULL)
	{
		printf("当前没有学生成绩信息!\n");
		return;
	}
	printf("请输入姓名:");
	scanf("%s",Name);
	while(p)
	{
		for(i=0;Name[i]!=0;i++)
		{
			if(Name[i]!=p->data.Name[i])
			break;
		}
		if(Name[i])
		p=p->next;
		else
		{
			printf("%.2f\n",p->data.Score);
			return ;	
		}
		
	}
	if (p==NULL)
	{
		printf("无当前学生信息\n");
		return;
	}
	
}

void Change(struct Node * Head)
{
	unsigned long ID;
	struct Node *m=Head,*n=Head,*d=Head;
	float Score;
	printf("请输入学号:");
	scanf("%lu",&ID);
	printf("请输入成绩:");
	scanf("%f",&Score);
	while(m)
	{
		if(ID==m->data.ID)
		{
			if(m==Head)
			{
				m->data.Score=Score;
				Head=m->next;
			
			}
			else if(m->next==NULL)
			{
				n->next=NULL;
				m->data.Score=Score;
			}
			else
			{
				d=m->next;
			    m->data.Score=Score;
			    n->next=d;
			}
			Head=Insert(Head,m);
			H=Head;
			return;
		}
		else
		{
			n=m;
			m=m->next;
		}	
	}
	if(m==NULL)
	printf("无当前学生信息\n");
	return;
}

int main()
{
	
	printf(" =========================================================\n");
 	printf("<                   学生管理系统                          >\n");
 	printf(" =========================================================\n");
 	printf("                       (请选择功能)\n(1)成绩录入 (2)显示全部(3)成绩查询 (4)成绩修改 (0)退出\n");
	int a=0;
	scanf("%d",&a);
	while (a)
	{

		switch(a)
		{
			case 1:
				Add(); 
				break;
			case 2:
				Display(H);
				break;
			case 3:
				Search(H);
				break;
			case 4:
				Change(H);
				break;
		
		}
    printf("                       (请选择功能)\n(1)成绩录入 (2)显示全部(3)成绩查询 (4)成绩修改 (0)退出\n");
	scanf("%d",&a);

	 } 
	 printf(" =========================================================\n");
 	return 0;
}

修改成绩并重新排序流程图

Created with Raphaël 2.2.0 找到要修改的结点 剪下此结点 修改剪下的结点的数据 插入原链表 yes

在这里插入图片描述

  • 28
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值