注册 登录
编程论坛 C++教室

[求助]关于链表的排序问题

kisscjy 发布于 2007-04-29 18:44, 435 次点击

假如有一条无序的链表

struct a
{int data;
a*next;
}

现在我想要对data进行排序,

建立新一条已经排好序的链表~~~

请问高位应该怎样写代码啊??

PS:本人乃一只菜菜鸟,希望各位高手帮帮忙!

2 回复
#2
nuciewth2007-04-29 20:03

将结点重新插入在头结点下.

找了个现成的C函数给你.

/****************************************************/
/* 直接插入排序 */
/****************************************************/

void Insert_Sort(node *head)
{
node *p,*pre,*s,*r;
p=head->next;
head->next=NULL;
while(p)
{
pre=p->next;
r=head;
s=head->next;
while(s&&s->info<p->info)
{
r=s;
s=s->next;
}
p->next=r->next;
r->next=p;
p=pre;
}
}

#3
kisscjy2007-04-29 23:46

谢谢楼上的解答~~~

让我先看看~~~

1