程序代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int xh;
int cj;
struct node *next;
}Node, *pNode;
void main()
{
pNode head = NULL, s, p, pre;
int i = 0;
while (i++ < 10)
{
s = (pNode)malloc(sizeof(Node));
s->next = NULL;
printf("第%d个学生(学号 成绩):", i);
scanf("%d%d", &s->xh, &s->cj);
if (head == NULL)
{
head = s; // 第一个学生
}
else
{
p = head;
pre = p;
while ((p != NULL) && (s->cj < p->cj))
{
pre = p;
p = p->next;
}
if (p == head)
{
s->next = head;
head = s;
}
else if (p == NULL)
{
pre->next = s;
}
else
{
s->next = pre->next;
pre->next = s;
}
}
}
printf("\n 输出结果: \n");
p = head;
while (p != NULL)
{
printf("(%d)-->%d \n", p->xh, p->cj);
p = p->next;
}
system("pause");
}
// run out:
/*
第1个学生(学号 成绩):1 69
第2个学生(学号 成绩):2 89
第3个学生(学号 成绩):3 59
第4个学生(学号 成绩):4 100
第5个学生(学号 成绩):5 68
第6个学生(学号 成绩):6 85
第7个学生(学号 成绩):7 82
第8个学生(学号 成绩):8 91
第9个学生(学号 成绩):9 72
第10个学生(学号 成绩):10 80
输出结果:
(4)-->100
(8)-->91
(2)-->89
(6)-->85
(7)-->82
(10)-->80
(9)-->72
(1)-->69
(5)-->68
(3)-->59
请按任意键继续. . .
*/