[求助]急救关于连表的排序?
希望大哥大姐,不吝赐教。小弟在次谢过。
struct student
{
char name[8];
long num;
int math_score;
int pro_score;
int total_score;
struct student *next;
};
以下是我写的连表中以数学成绩的排序算法请大家帮我看一看。如果你有更好的方法,不吝赐教。小弟再次谢过。
我的基本思路是先将一个连表里的所有内容装到结构体数组中,然后对结构体数组中按数学成绩排序(冒泡法),最后再将结构体数组中的所有内容装到连表中去。
struct student a[100];/*定义一个结构体数组*/
struct student *p;
int j;
int i;
char name1[8];
long num1;
int math_score1;
int pro_score1;
int total_score1;
if(n==0)printf("this head is empty\n");
else
{
p=head;
for(i=1;i<=n;i++,p++)
{
a[i].num=p->num;
a[i].math_score=p->math_score;
a[i].pro_score=p->pro_score;
a[i].total_score=p->total_score;
strcpy(a[i].name,p->name);
}
for(i=1;i<n;i++)
for(j=i+1;j<n-i;j++)
{
if(a[i].math_score<a[j].math_score)
{
strcpy(name1,a[i].name);
num1=a[i].num;
math_score1=a[i].math_score;
pro_score1=a[i].pro_score;
total_score1=a[i].total_score;
strcpy(a[i].name,a[j].name);
a[i].num=a[j].num;
a[i].math_score=a[j].math_score;
a[i].pro_score=a[j].pro_score;
a[i].total_score=a[j].total_score;
strcpy(a[j].name,name1);
a[j].num=num1;
a[j].math_score=math_score1;
a[j].pro_score=pro_score1;
a[j].total_score=total_score1;
}
}
p=head;
for(i=1;i<=n;i++,p++)
{
p->num=a[i].num;
strcpy(p->name,a[i].name);
p->math_score=a[i].math_score;
p->pro_score=a[i].pro_score;
p->total_score=a[i].total_score;
}
}
for(i=1;i<=n;i++) /*以下是我用来检查次段程序的*/
printf("%ld,%s,%d,%d,%d\n",a[i].num,a[i].name,a[i].math_score,a[i].pro_score,a[i].total_score);