单链表排序解决方法
单链表排序
我写了个链表:
#include "stdio.h "
#include "string.h "
#define N 10
extern _floatconvert;
#pragma extref _floatconvert /*Solve the problem of floating scanf*/
typedef struct student
{
int num;
char name[20];
char gender;
int age;
float chinese;
float maths;
float english;
float aver;
struct student *next;
}stu;
main()
{
stu *sort(stu *);
int i;
stu *p,s[N]={{107, "xuhui ", 'm ',28,90.00,95.00,97.00},
{102, "jiangyi ", 'm ',25,92.00,95.00,96.00},
{103, "jiangchangwei ", 'm ',28,75.00,60.00,65.00},
{104, "wenkai ", 'm ',28,60.00,65.00,60.00},
{101, "huangqinyu ", 'f ',28,90.00,65.00,75.00}};
for(p=s;p <s+N;p++)
{
p-> aver=(p-> chinese+p-> maths+p-> english)/3.00;
p-> next=p+1;
}
p--;
p-> next=NULL;
p=sort(s);
while(p)
{
printf( "%d %s %c %d %.2f %.2f %.2f %.2f\n ",p-> num,p-> name,p-> gender,p-> age,p-> chinese,p-> maths,p-> english,p-> aver);
p=p-> next;
}
getch();
}
stu *sort(stu *stud)
{
stu *head,*phead,*p4,*p1,*p2;
phead=head=stud;
p4=phead-> next;
while(p4)
{
p2=head;
while(p2-> aver> p4-> aver) 如果这里用aver(平均分),num,english都可得到
正确结果,
{ 但用别的就不行(如maths),不知道是怎么回事,
p1=p2; 望高手解答!
p2=p2-> next;
}
if(p2!=p4)
{
phead-> next=p4-> next;
if(p2==head)
{
p4-> next=p2;
我写了个链表:
#include "stdio.h "
#include "string.h "
#define N 10
extern _floatconvert;
#pragma extref _floatconvert /*Solve the problem of floating scanf*/
typedef struct student
{
int num;
char name[20];
char gender;
int age;
float chinese;
float maths;
float english;
float aver;
struct student *next;
}stu;
main()
{
stu *sort(stu *);
int i;
stu *p,s[N]={{107, "xuhui ", 'm ',28,90.00,95.00,97.00},
{102, "jiangyi ", 'm ',25,92.00,95.00,96.00},
{103, "jiangchangwei ", 'm ',28,75.00,60.00,65.00},
{104, "wenkai ", 'm ',28,60.00,65.00,60.00},
{101, "huangqinyu ", 'f ',28,90.00,65.00,75.00}};
for(p=s;p <s+N;p++)
{
p-> aver=(p-> chinese+p-> maths+p-> english)/3.00;
p-> next=p+1;
}
p--;
p-> next=NULL;
p=sort(s);
while(p)
{
printf( "%d %s %c %d %.2f %.2f %.2f %.2f\n ",p-> num,p-> name,p-> gender,p-> age,p-> chinese,p-> maths,p-> english,p-> aver);
p=p-> next;
}
getch();
}
stu *sort(stu *stud)
{
stu *head,*phead,*p4,*p1,*p2;
phead=head=stud;
p4=phead-> next;
while(p4)
{
p2=head;
while(p2-> aver> p4-> aver) 如果这里用aver(平均分),num,english都可得到
正确结果,
{ 但用别的就不行(如maths),不知道是怎么回事,
p1=p2; 望高手解答!
p2=p2-> next;
}
if(p2!=p4)
{
phead-> next=p4-> next;
if(p2==head)
{
p4-> next=p2;