大家帮我看看我的链表排序为什么有问题

tianshilei1992 2012-11-22 05:07:49
现在有一个链表,想对其进行排序,我用的选择排序,但是这个函数有时候好使,有时候不好使,不知道怎么回事,大家帮忙看一下。代码如下:

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;
}
}
...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshilei1992 2012-11-25
  • 打赏
  • 举报
回复
引用 6 楼 HONDELY 的回复:
C/C++ code?12345678910111213141516171819202122232425110.Node *Sorted(Node *head)//简单的冒泡排序法 111.{ 112. Node *p1,*p2; 113. p1=head; 114. p2=head; 115. int len=Length(head);……
嗯嗯,我最后用冒泡重新写了一个~
hondely 2012-11-24
  • 打赏
  • 举报
回复
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||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.}
tianshilei1992 2012-11-23
  • 打赏
  • 举报
回复
引用 2 楼 HONDELY 的回复:
C/C++ code?1<a href="http://blog.csdn.net/hondely/article/details/8115251">http://blog.csdn.net/hondely/article/details/8115251</a>
谢谢,昨天我发现我这写的压根就不是个排序……这最后没效果啊……怪不得能跑出死循环来%
tianshilei1992 2012-11-23
  • 打赏
  • 举报
回复
引用 1 楼 logiciel 的回复:
链表中节点交换后原来用变量保存的前后关系变了,好像不能继续用于循环,重新开始容易处理C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940rearrange_FF() { struct free_block_type *pre1, *pre2, *count1,……
我发现我写的这个压根就不是个排序啊……我昨天发帖完了研究了一下,这根本就没法排序貌似……
hondely 2012-11-23
  • 打赏
  • 举报
回复

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;
    }
}
hondely 2012-11-23
  • 打赏
  • 举报
回复

http://blog.csdn.net/hondely/article/details/8115251
logiciel 2012-11-22
  • 打赏
  • 举报
回复
链表中节点交换后原来用变量保存的前后关系变了,好像不能继续用于循环,重新开始容易处理
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);//交换过再重新开始循环
} 

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧