| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:链表排序问题
只看楼主 加入收藏
FireRabbit
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2014-8-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
链表排序问题
我想通过修改节点的下一个指向来改变它在链表中的位置,代码如下:

    while(s2!=NULL)
    {
        if((s1->buy_number)>(s2->buy_number))
        {
            s1->next=s2->next;
            s2->next=s1;

        }
        s1=s1->next;
        s2=s2->next;

    }

排序按照buy_number从小到大的顺序进行排列。
然而运行结果似乎进入了一个无限循环?
*出问题的部分已用黑体标注



[ 本帖最后由 FireRabbit 于 2014-12-24 15:24 编辑 ]
2014-12-24 15:15
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:10 
I wonder what's the relationship of s1 and s2?
s1->next==s2 ?


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-12-24 15:54
FireRabbit
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2014-8-6
收藏
得分:0 
回复 2楼 蚕头燕尾
s1、s2 is the pointer of the struct

    s1=head;
    s2=head->next;

    while(s2!=NULL)
     {
         if((s1->buy_number)>(s2->buy_number))
         {
            s1->next=s2->next;
             s2->next=s1;
         }
         s1=s1->next;
         s2=s2->next;

     }



[ 本帖最后由 FireRabbit 于 2014-12-24 16:26 编辑 ]

我的愿望是用C++开发自己的游戏引擎。
在此之前还有许多东西要学……
2014-12-24 16:23
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
1.what's the error information? Can you copy to here?
2.Do you know this:
after those codes"s1->next=s2->next; s2->next=s1;", you can paint a picture, then you will find out an interesting
thing, "s2->next==s1"-->this will be true, which means before you run those codes, the relationship of s1 and s2
is this "s1->next==s2", right? BUT! after you run those codes the relationship is changed!
Is this you expected? I think the answer is no, because you have set a line to stop the circle like this "while(s2!=NULL)"
if you are not sure the relationship of s1 and s2,(I mean, which one is faster to get the end of the line), how can you set
the stop situation like "s2!=NULL"? right?
--------------------
all in all,  "s1->next=s2->next; s2->next=s1;"-->those codes should be wrong in logic.
Please rethink those codes.



[ 本帖最后由 蚕头燕尾 于 2014-12-24 19:35 编辑 ]
收到的鲜花
  • FireRabbit2014-12-25 12:26 送鲜花  2朵   附言:thank u!

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-12-24 19:33
巧若拙
Rank: 4
来 自:宁波余姚
等 级:业余侠客
威 望:1
帖 子:159
专家分:273
注 册:2014-8-24
收藏
得分:10 
缺乏一个语句。
试问,当
s1->next=s2->next;
s2->next=s1;
执行完毕,谁是s2的前驱结点?
2014-12-25 08:13
巧若拙
Rank: 4
来 自:宁波余姚
等 级:业余侠客
威 望:1
帖 子:159
专家分:273
注 册:2014-8-24
收藏
得分:0 
一个对单链表线性表进行插入排序的算法,其中用到一个小技巧,不用定位插入位置的前驱结点,很巧妙哦!
/*
函数功能:给单链表线性表排序
初始条件:单链表线性表L已经存在,1 <= i <= ListLength(L);
操作结果:使用插入排序的方法,为单链表线性表排序。
操作成功返回OK,否则返回ERROR
*/
Status ListSort(LinkList *L)
{
ElemType temp;
LinkList q, p, pos = (*L)->next; //p指向第1个结点

if (!pos)
{
printf("空表!");
return ERROR;
}
while (pos->next != NULL)//寻找最后一个结点
{
pos = pos->next;
}

while ((*L)->next != pos)
{
q = (*L)->next;
(*L)->next = q->next;
p = pos;
while (p->next != NULL && p->data < q->data) //寻找插入点
{
p = p->next;
}

q->next = p->next; //在p后插入q
p->next = q;

if (p->data > q->data) //若p的值大于q,交换二者的值,以保证线性表有序
{
temp = p->data;
p->data = q->data;
q->data = temp;
}

}

return OK;
}
收到的鲜花
  • FireRabbit2014-12-25 12:27 送鲜花  2朵   附言:多谢~
2014-12-25 08:16
快速回复:链表排序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015632 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved