链表的排序!

来源:百度知道 编辑:UC知道 时间:2024/05/30 17:34:58
struct student *hebing(struct student *head1,struct student *head2) //合并A,B链表
{
struct student *p1,*p2,*temp,*pt;
int hao;
p1=head1;
p2=head2;
while(p1->next!=NULL) //合并
p1=p1->next;

p1->next=p2;//
p1=head1;
hao=p1->num; //排序
for(p1=head1;p1->next!=NULL;p1=p1->next)
{pt=p1;
while(pt->next!=NULL)
{
if(p1->num<hao)
{
temp=p1;
p1=pt;
pt=temp;
}
}
}
p1=head1;
return(head1);
}

这是错的,怎样改才对啊,我的思路对吗?
C语言的。。不知道为什么发到这里了!-.-#

你说的那个书上的程序我给你解释了,你自己写的这个错误比较多,我又没有全部的原代码,也不好给你调.我给你指几个错误,你自己想办法改吧.
1:hao=p1->num; //排序 (这一句不应该用这,应该放在for(..)后面,因为hao的值需要不断变化

2:while(pt->next!=NULL){ } (这个循环中,应该加一句pt=pt->next,否则死循环)

3:if(p1->num<hao){ temp=p1; p1=pt; pt=temp; } 这句中p1应改成pt,此处的p1是一个控制循环的变量,你不能随便改变它的值,不然影响后面的循环.

哎——
看不懂ye!!