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

在这里插入图片描述

Logo

快速构建 Web 应用程序

更多推荐