儿童女游泳衣:链表排序问题~急~~

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 09:34:08
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};

struct student *creat(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for (i=1;i<=n;i++)
{
p1=(struct student *)malloc(LEN);
scanf ("%ld",&p1->num);
scanf ("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while ((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if (p0->num<=p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {p1->next=p0;p0->next=NULL;}
}
return(head);
}
struct student *sort(struct student *head)
{
struct student *temp,*p1,*p2;
p1=head;
temp=NULL;
for(;p1->next!=NULL;p1++)
{
p2=p1->next;
while(p2->next!=NULL)
{
if(p1->num>p2->num)
{
temp=p1;
p1=p2;
p2=temp;
}
p2++;
}

}
return(head);
}
main()
{
int n;
struct student *head,*stu;
scanf ("%d",&n);
head=creat(n);
print(head);
head=sort(head);
print(head);
}

按学号从小到大排序,请帮忙看看SORT函数是不是出问题了
我是刚学的~能帮我改改吗???

这种写法有问题,你这样子根本就没有改了任何东西,
temp=p1;
p1=p2;
p2=temp;
要象这样子写*temp->next=p2;
你要注意前一个节点的next改成指向下一个节点。

而且你一个循环也不可能排好序的。