大家帮我看看我的链表排序为什么有问题
现在有一个链表,想对其进行排序,我用的选择排序,但是这个函数有时候好使,有时候不好使,不知道怎么回事,大家帮忙看一下。代码如下:
void
rearrange_FF()
{
struct free_block_type *pre1, *pre2, *count1, *count2, *tmp1, *tmp2;
pre1 = free_block_head;
for (count1 = free_block_head -> next; count1 != NULL; count1 = count1 -> next)
{
pre2 = count1;
for (count2 = count1 -> next; count2 != NULL; count2 = count2 -> next)
{
if (count1 -> start_addr > count2 -> start_addr)
{
tmp1 = count1 -> next;
tmp2 = count2 -> next;
pre1 -> next = count2;
pre2 -> next = count1;
count1 -> next = tmp2;
count2 -> next = tmp1;
}
pre2 = pre2 -> next;
}
pre1 = pre1 -> next;
}
}
rearrange_FF()。
{
struct free_block_type *pre1, *pre2, *count1, *count2, *tmp1, *tmp2;
int switched;
do
{
switched = 0;
pre1 = free_block_head;
for (count1 = free_block_head -> next; count1 != NULL && !switched; count1 = count1 -> next)
{
pre2 = count1;
for (count2 = count1 -> next; count2 != NULL; count2 = count2 -> next)
{
if (count1 -> start_addr > count2 -> start_addr)
{
tmp1 = count1 -> next;
tmp2 = count2 -> next;
pre1 -> next = count2;
if (tmp1 == count2) //交换2个相邻节点
{
count2 -> next = count1;
count1 -> next = tmp2;
count2 = count1;
}
else//交换2个不相邻节点
{
pre2 -> next = count1;
count1 -> next = tmp2;
count2 -> next = tmp1;
}
switched = 1;//交换后置标志switched为1,从而退出循环
break;
}
pre2 = pre2 -> next;
}
pre1 = pre1 -> next;
}
} while(switched);//交换过再重新开始循环
}
110.Node *Sorted(Node *head)//简单的冒泡排序法
111.{
112. Node *p1,*p2;
113. p1=head;
114. p2=head;
115. int len=Length(head);
116. if (NULL==head
[其他解释]
http://blog.csdn.net/hondely/article/details/8115251
[其他解释]
void //怎么能事返回空????返回头指针 地址变了 应该引用
rearrange_FF()
{
struct free_block_type *pre1, *pre2, *count1, *count2, *tmp1, *tmp2;
pre1 = free_block_head;
for (count1 = free_block_head -> next; count1 != NULL; count1 = count1 -> next)
{
pre2 = count1;
for (count2 = count1 -> next; count2 != NULL; count2 = count2 -> next)
{
if (count1 -> start_addr > count2 -> start_addr)
{
tmp1 = count1 -> next;
tmp2 = count2 -> next;
pre1 -> next = count2;
pre2 -> next = count1;
count1 -> next = tmp2;
count2 -> next = tmp1;
}
pre2 = pre2 -> next;
}
pre1 = pre1 -> next;
}
}
[其他解释]