#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
float score;
struct student* next;
};
void print(struct student* head);
struct student* creatsort();
int main()
{
struct student* head;
head = creatsort();
print(head);
return 0;
}
struct student* creatsort()
{
struct student* head = NULL;
struct student* pnew, * back, * front;
int num;
float score;
printf("请输入成绩:\n");
scanf_s("%d,%f", &num, &score);
if (num < 0)
{
return head;
}
while (num >= 0)
{
pnew = (struct student*)malloc(sizeof(struct student));
pnew->num = num;
pnew->score = score;
if (head == NULL)
{
head = pnew;
pnew->next = NULL;
}
else
{
back = front = head;
if (head->score < score)
{
pnew->next = head;
head = pnew;
}
else
{
front = back = head;
while (front != NULL && front->score > score)
{
back = front;
front = front->next;
}
if (front == NULL && front->score > score)
{
pnew->next = back->next;
front->next = pnew;
}
else
{
pnew->next = front->next;
front->next = pnew;
}
}
}
printf("新排名如下:\n");
print(head);
printf("请输入新成绩:\n");
scanf_s("%d,%f", &num, &score);
}
return head;
}
void print(struct student* head)
{
struct student* p;
p = head;
if (p == NULL)
{
printf("Empty!\n");
return;
}
while (p != NULL)
{
printf("%d,%.2f\n", p->num, p->score);
p = p->next;
}
}
如上图,程序在第三次输入后就不再输出,莫名奇妙的就终止了。