C语言链表排序

来源:百度知道 编辑:UC知道 时间:2024/06/14 17:55:47
帮忙检查一下代码的错误!谢谢!(贴出的代码为排序函数部分)
有结构体类型定义
struct student
{ long num;
int score;
struct student *next;
}
我的代码:
struct student *sort(struct student *head)
{ int n=0;
struct student *p,*p0,*p1,*p2,*p3,*head1;
p=head;p2=head;p1=head->next;p3=NULL;p0=NULL;
while(head!=NULL)
{++n;
while(p1->next!=NULL&&p1!=NULL) {p2=p1;p1=p1->next;
if(p1->num<p->num) {p=p1;p0=p2;}
}
;
if(n==1) {if(p==head) {head1=head;p3=head1;head=head->next;}
else{head1=p;p0->next=p->next;p3=p;}
head1->next=NULL;
}
else{if(p==head) {p3->next=p;head=p->next;}
else{p3->next=p;p0->next=p->next;}
p->next=NULL;p3=p;
}
}
return(head1);
}

struct student *sort(struct student *head)
{ int n=0,temp2;
float temp1;
struct student *p1,*p2;
p2=head;p1=head->next;
if(head==NULL)
{
printf("the list is empty!");
}
while(p1->next!=NULL&&p1!=NULL)
{
p2=p1;p1=p1->next;
if(p1->num<p2->num) {temp1=p1->score;p1->score=p2->score;p2->score=temp1;temp2=p1->num;p1->num=p2->num;p2->num=temp2;}
}
return(head);
}
你为什么用那么多的指针啊.你比较过后,直接交换他们的值就行了啊.
你看看用我这个能实现你要的功能吗?要是不能的话,向我百度发消息.
说说你的主要目的.