创建链表存储学生信息,一个结点存储一个学生的信息,包括姓名、年龄、学号、分数。要求手动输入创建链表的长度,然后创建动态链表,并从键盘给每一个结点赋值。根据学生的成绩从高到低对链表结点进行排序,最后输出排序后的整个链表
#include<stdio.h>
#include<stdlib.h>
struct STUDENT
{
char name[20] ;
int age ;
float score ;
char num[20] ;
};
struct NODE
{
struct STUDENT data ;
struct NODE * next ;
};
struct NODE * CreateLink(void);
void IntputLink(struct NODE * );
void BubbleSort(struct NODE * );
void OutputLink(struct NODE * );
int main ()
{
struct NODE * head = CreateLink();
IntputLink( head ) ;
BubbleSort( head);
printf("排序后的结果如下:\n");
OutputLink(head);
return 0 ;
}
struct NODE * CreateLink()
{
int i = 0 ;
int cnt = 0 ;
struct NODE * head = malloc ( sizeof * head );
struct NODE * move = head ;
move->next = NULL ;
printf("请输入学生的数量:");
scanf("%d",&cnt);
getchar();
for ( i = 0 ; i < cnt ; i ++ )
{
struct NODE * fresh = malloc ( sizeof * fresh );
move->next = fresh ;
fresh->next = NULL ;
move = fresh ;
}
return head ;
}
void IntputLink( struct NODE * head )
{
int i = 1 ;
struct NODE * move = head->next ;
while ( NULL != move )
{
printf("请输入第%d个学生的姓名、年龄、成绩、学号:",i);
scanf("%s %d %f %s",move->data.name , &move->data.age , &move->data.score , move->data.num );
getchar();
move = move->next ;
i ++ ;
}
return ;
}
void BubbleSort( struct NODE * head )
{
struct NODE * move = head->next ;
struct NODE * turn = head->next ;
struct STUDENT buf ;
if ( NULL == move )
{
printf("链表为空!\n");
exit(0);
}
while ( NULL != turn->next )
{
while ( NULL != move->next )
{
if ( move->data.score < move->next->data.score )
{
buf = move->data ;
move->data = move->next->data ;
move->next->data = buf ;
}
move = move->next ;
}
move = head->next ;
turn = turn->next ;
}
return ;
}
void OutputLink( struct NODE * head )
{
struct NODE * move = head->next ;
while ( NULL != move )
{
printf("【姓名:%s、年龄:%d、成绩:%.2f、学号:%s】\n",move->data.name , move->data.age , move->data.score , move->data.num );
move = move->next ;
}
return ;
}
请输入学生的数量:5
请输入第1个学生的姓名、年龄、成绩、学号:欧阳丽 24 83 z1207038
请输入第2个学生的姓名、年龄、成绩、学号:冯小莉 20 80 z1207025
请输入第3个学生的姓名、年龄、成绩、学号:陈商清 23 85 z1207024
请输入第4个学生的姓名、年龄、成绩、学号:陈迎接 26 84 Z1207043
请输入第5个学生的姓名、年龄、成绩、学号:周琴琴 25 91 Z1207041
排序后的结果如下:
【姓名:周琴琴、年龄:25、成绩:91.00、学号:Z1207041】
【姓名:陈商清、年龄:23、成绩:85.00、学号:z1207024】
【姓名:陈迎接、年龄:26、成绩:84.00、学号:Z1207043】
【姓名:欧阳丽、年龄:24、成绩:83.00、学号:z1207038】
【姓名:冯小莉、年龄:20、成绩:80.00、学号:z1207025】
Program ended with exit code: 0