读书人

大家帮小弟我看看小弟我的链表排序为什

发布时间: 2012-12-16 12:02:32 作者: rapoo

大家帮我看看我的链表排序为什么有问题
现在有一个链表,想对其进行排序,我用的选择排序,但是这个函数有时候好使,有时候不好使,不知道怎么回事,大家帮忙看一下。代码如下:


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;
}
}


[其他解释]
引用:
链表中节点交换后原来用变量保存的前后关系变了,好像不能继续用于循环,重新开始容易处理C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940rearrange_FF() { struct free_block_type *pre1, *pre2, *count1,……

我发现我写的这个压根就不是个排序啊……我昨天发帖完了研究了一下,这根本就没法排序貌似……
[其他解释]
引用:
C/C++ code?1<a href="http://blog.csdn.net/hondely/article/details/8115251">http://blog.csdn.net/hondely/article/details/8115251</a>

谢谢,昨天我发现我这写的压根就不是个排序……这最后没效果啊……怪不得能跑出死循环来%
[其他解释]
NULL==head->next)
117. return head;
118. for (int i=0; i<len; ++i)
119. {
120. p2=p1;
121. for (int j=i+1; j<len; ++j)
122. {
123. if(p1->data>p2->next->data)
124. {
125. p1->data^=p2->next->data;
126. p2->next->data^=p1->data;
127. p1->data^=p2->next->data;
128. }
129. p2=p2->next;
130. }
131. p1=p1->next;
132. }
133. return head;
134.}

读书人网 >C语言

热点推荐