#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
typedef long LElemType; //学号变量类型
typedef float FElemType; //成绩变量类型
typedef int Status; //返回值的类型
typedef struct student
{
LElemType num;
FElemType score;
student *next;
}node;
typedef struct LIST
{
node *head,*tail;
}link;
node *MakeNode(long num, float score) //生成存放学生信息的节点
{
node *p=(node *)malloc(sizeof(node));
p->num = num; //存放学号
p->score = score; //存放分数
p->next = NULL; //刚生成的节点下一个为空
return p;
}
Status Create(link *s,int n)
{
long num;
float score;
s->head = s->tail = MakeNode(0,-1); //头结点num用来存放学生信息条数。score空着不用
if(NULL == s->head)
{
puts("分配失败!");
return ERROR;
}
for(int i=0; i<n; i++) //录入n个学生的信息
{
printf("请输入第%d个学生的信息(学号与成绩): ",i);
scanf("%d%f",&num,&score);
node *p = MakeNode(num,score);
s->tail->next = p;
s->tail = p;
s->head->num++; //修改头结点中存放学生信息条数每增加一个节点就要++1
}
return OK;
}
Status display(link *s)
{
node *p = s->head->next; //让p指针指向头结点的下一个节点,因为头结点只用来存放了学生的人数
while(p)//只要p指针指向不为空就要输出
{
printf("%d %.2f\n",p->num,p->score);
p = p->next;
}
return OK;
}
//按照成绩升序排序
Status SortByScore(link *s)
{
int n = s->head->num;
node *p,*cur,*next;
for(int i=0; i<n-1; i++)
{
p= s->head;
cur=p->next;
next = cur->next;
for(int j=0; j<n-1-i; j++)
{
if(next->score<cur->score)
{
p->next=next;
cur->next = next->next;
next->next = cur;
p=next;
next=cur->next;
}else{
p=cur;
cur = next;
next = next->next;
}
}
}
return OK;
}
int main()
{
link s;
int n;
printf("你想录入多少学生的记录:n=");
scanf("%d",&n);
Create(&s,n);
puts("所有学生的信息(学号与成绩): ");
display(&s);
SortByScore(&s);
puts("按成绩升序排序好的所有学生的信息(学号与成绩): ");
display(&s);
return OK;
}
lianbiao.zip_site:www.pudn.com
版权申诉
60 浏览量
2022-09-23
12:02:11
上传
评论
收藏 203KB ZIP 举报
钱亚锋
- 粉丝: 90
- 资源: 1万+
最新资源
- (完整)数据库课程设计餐厅点餐说明书-21ab6d3c8beb172ded630b1c59eef8c75ebf952c.doc
- 2023-04-06-项目笔记 - 第一百五十四阶段 - 4.4.2.152全局变量的作用域-152 -2024.06.04
- 松哥解协议松哥解协议松哥解协议松哥解协议松哥解协议
- 618节日618节日618节日
- tensorflow-gpu-2.9.1-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp39-cp39-win-amd64.whl
- lcd daimalcd daima
- 电影领域-推荐算法-个性化内容-观影决策-电影推荐小程序.zip
- 电气控制PLC考试题库
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈