//题目:输入n个同学的成绩,并且排序
//问题1:排序完最后一个数据变成 0 了
//问题2:如果在12行后面加一行 L = head; 为什么就不出结果了?
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{
int data;
struct student *next;
}a;
struct student *p,*q,*head;
void f(struct student *L,int n)
{
int t,i,j;
p=L;
q = p->next;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p->data < q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
p = q;
q = q->next;
}
p = L;
q = p->next;
}
}
int main()
{
struct student *p1;
p1=&a;
int i,n,c=0;
printf("请输入n:\n");
scanf("%d",&n);
q=&a;
printf("\n");
q->next=NULL;
printf("请输入%d个同学的成绩:\n",n);
for(i=0;i<n;i++)
{
p = (struct student*)malloc(sizeof(struct student));
c++;
if(c==1) head = p;
scanf("%d",&p->data);
q->next = p;
q = q->next;
}
f(p1,n);
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\n", head->data);
head = head->next;
}
return 0;
}