成绩排序

  

题目描述

有N个学生,每个学生的数据包括学号、姓名、成绩,要求按成绩大小输出学生的姓名(若成绩相等,学号小的排名在前)

输入

第一行为学生数量N

接下来的N行为每个学生的学号、姓名、成绩

输出

学生姓名,每行一个

样例输入

4
4 jx 97
2 ust 90
3 acm 97
1 oj 89

样例输出

acm
jx
ust
oj

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 //创建STUDENT类型变量 包括学号、姓名、成绩
 5 typedef struct student {
 6     int  number;
 7     char name[50];
 8     int  score;
 9     struct  student *pNext;
10 }STUDENT;
11 //创建链表
12 STUDENT *Create(int n) {
13     STUDENT *pHead, *pEnd, *pNew = NULL;
14     int i; 
15     pHead = pEnd = (STUDENT*)malloc(sizeof(STUDENT));
16     for (i = 0; i < n; i++)
17     {
18         pNew = (STUDENT*)malloc(sizeof(STUDENT));
19         scanf("%d%s%d", &pNew->number, pNew->name, &pNew->score);
20         
21         pNew->pNext = NULL;
22         pEnd->pNext = pNew;
23         pEnd = pNew;
24 
25     }
26     return pHead;
27 }
28 //寻找前驱节点
29 STUDENT *Find_before(STUDENT* phead, STUDENT* p)
30 {
31     if (!p) return NULL;
32     STUDENT *pbefore = phead;
33     while (pbefore)
34     {
35         if (pbefore->pNext == p)
36             return pbefore;
37         pbefore = pbefore->pNext;
38     }
39     return NULL;
40 }
41 
42 STUDENT *Sort(STUDENT *head) {
43     STUDENT *pHead,*pEnd,*q=NULL,*qbefore=NULL,*p=NULL;
44     int maxscore,minnumber;
45     pHead=pEnd = (STUDENT*)malloc(sizeof(STUDENT));
46     while (head->pNext != NULL)
47     {
48         maxscore = head->pNext->score;
49         minnumber = head->pNext->number;
50         q = p = head->pNext;
51         //寻找最高分且最小学号的节点
52         while (p->pNext!=NULL)
53         {
54             if(maxscore<p->pNext->score)
55             {
56                 maxscore = p->pNext->score; minnumber = p->pNext->number;  q = p->pNext;
57             }
58          if ((maxscore == p->pNext->score) && (minnumber > p->pNext->number))
59             {
60              maxscore = p->pNext->score; minnumber = p->pNext->number; q = p->pNext;
61             }
62             p = p->pNext;    
63         }
64     qbefore = Find_before(head, q);  //寻找q节点的前驱节点
65     qbefore->pNext = q->pNext; //将q的前驱节点指向q的后驱节点 从而将q节点从a链表中剔除
66         
67 pEnd->pNext = q;    //将头指针指向q
68         q->pNext = NULL;    //q节点指向空
69         pEnd = q;            //更新尾节点
70         
71     }
72     free(head);//释放head链表头节点
73     return pHead;
74 }
75 
76 void print(STUDENT *q) {
77     while (q->pNext != NULL)
78     {
79         q = q->pNext; printf("%s
", q->name);
80     }
81     free(q);//释放使用完的链表
82 }
83 int main() {
84     int n; STUDENT *p,*q;
85     scanf("%d", &n);
86     p = Create(n);
87     q=Sort(p);
88     print(q);
89 }
原文地址:https://www.cnblogs.com/mwq1024/p/10228693.html