| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 438 人关注过本帖
标题:程式问题
收藏  订阅  推荐  打印
suckdog
Rank: 2
等级:注册会员
帖子:46
积分:686
注册:2007-9-19
程式问题

我这个程式现在要按照从小到大的顺序排应该怎么弄呢? 现在它好像不能按顺序排


struct nodetype
{
    int adata;
    struct nodetype *ptr;
};

int main()
{
      struct nodetype *start, *lastentry, *entry, *newnode, *first, *second;
      int info, akey, aflag;

      start=new nodetype;
      start->ptr=NULL;
      lastentry=start;

      cout<<"input an integer value, '999' to exit";
      cout<<endl;
      cin>>info;
      cout<<endl;
      while(info != 999)
      {
        newnode=new nodetype;
        newnode->adata = info;
        newnode->ptr = NULL;
        lastentry->ptr = newnode;
        lastentry= newnode;
        cout<<"input an integer value, '999' to exit";
        cout<<endl;
        cin>>info;
        cout<<endl;
      }

      entry=start->ptr;
      cout<<"you just entered:\n";
      while (entry!=NULL)
      {
        cout<<entry->adata<<endl;
        entry=entry->ptr;

      }
      cout<<endl;

      do                            // 这里就是我做排列顺序的地方, 帮我从新便一下, 我不知哪里不对
      {
        aflag=0;
        first=start->ptr;
        second=first->ptr;
        cout<<"after sorting:\n";
            if(first->adata>second->adata)
            {
                aflag++;
                first=first->ptr;
                second=second->ptr;
            }
            else if (first->adata<=second->adata)
            {
                first=first->ptr;
                second=second->ptr;
            }
      }while((aflag==1)&&(second->ptr!=NULL));

      entry=start->ptr;
      while (entry!=NULL)
      {
        cout<<entry->adata<<endl;
        entry=entry->ptr;

      }
      cout<<endl;


      cout<<endl<<"Enter a number you want to search."<<endl;
      cin>>akey;
      entry=start->ptr;
      while ((akey != entry->adata) && (entry->ptr != NULL))
      {
        entry=entry->ptr;
      }
      if (akey==entry->adata)
      {
        cout<<"found"<<endl;
      }
      else
      {
        cout<<"not found"<<endl;
      }
      system("PAUSE");
      return 0;
}
搜索更多相关主题的帖子: 程式  
2008-5-17 12:58
suckdog
Rank: 2
等级:注册会员
帖子:46
积分:686
注册:2007-9-19

自己顶一下
2008-5-18 09:29
中学者
Rank: 12Rank: 12Rank: 12
等级:版主
威望:11
帖子:3375
积分:34540
注册:2007-9-14

链表排序么????可以直接交换结点值而不修改结点的连接顺序....

汇编.....
2008-5-18 09:57
Aegisys
Rank: 1
等级:新手上路
帖子:19
积分:302
注册:2008-5-10

晕,把First和Second两个指针搞了一通,好像链表还是原样...
2008-5-18 13:17
suckdog
Rank: 2
等级:注册会员
帖子:46
积分:686
注册:2007-9-19

对,现在是原样,现在谁把那个地方从写一下,我看看哪里不对, 光说不清楚。
2008-5-19 05:19
dubaoshi
Rank: 2
等级:注册会员
帖子:115
积分:1296
注册:2006-9-22

我周六看了好久,加上今天又调了2个小时,终于算是成功了,但肯定效率不高:(
你先试一下吧。
排序部分我是这样改的:
      do                            // 这里就是我做排列顺序的地方, 帮我从新便一下, 我不知哪里不对
      {

            if(first->adata>second->adata)
            {

                temp=first->adata;//加了个临时变量temp
                first->adata=second->adata;
                second->adata=temp;
                first=start->ptr;//在这里就又可以从头对比了
                second=first->ptr;
            }
            else//这里就不加东西了
            {
                first=first->ptr;
                second=second->ptr;
            }
      }while(second!=NULL);这里把标志给去掉了


累死啦~~~

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-5-20 11:47
dubaoshi
Rank: 2
等级:注册会员
帖子:115
积分:1296
注册:2006-9-22

虽然是能运行了,但这个链表我还都不大懂:(
用new建的这个链表在退出程序时,并没有回收内存啊,呵呵~~

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-5-20 11:53
suckdog
Rank: 2
等级:注册会员
帖子:46
积分:686
注册:2007-9-19

你的程式我试了一下,成功一般, 我输入 3,2,1, 它输出 2,3,1, 很明显没有回头继续排列
2008-5-20 12:11
dubaoshi
Rank: 2
等级:注册会员
帖子:115
积分:1296
注册:2006-9-22


怎么会呢?在我这里是完全正常的啊。
如下动画所示:

有问题再交流啊~
附件: 只有本站会员才能下载或查看附件,请您 登录注册

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-5-20 13:28
dubaoshi
Rank: 2
等级:注册会员
帖子:115
积分:1296
注册:2006-9-22

完整程序如下:
#include<iostream>

using namespace std;
struct nodetype
{
    int adata;
    struct nodetype *ptr;
};

int main()
{
      struct nodetype *start, *lastentry, *entry, *newnode, *first, *second;
      int info, akey, aflag,temp;

      start=new nodetype;
      start->ptr=NULL;
      lastentry=start;

      cout<<"input an integer value, '999' to exit";
      cout<<endl;
      cin>>info;
      cout<<endl;
      while(info != 999)
      {
        newnode=new nodetype;
        newnode->adata = info;
        newnode->ptr = NULL;
        lastentry->ptr = newnode;
        lastentry= newnode;
        cout<<"input an integer value, '999' to exit";
        cout<<endl;
        cin>>info;
        cout<<endl;
      }

      entry=start->ptr;
      cout<<"you just entered:\n";
      while (entry!=NULL)
      {
        cout<<entry->adata<<endl;
        entry=entry->ptr;

      }
      cout<<endl;

        first=start->ptr;
        second=first->ptr;
        cout<<"after sorting:\n";

      do                            // 这里就是我做排列顺序的地方, 帮我从新便一下, 我不知哪里不对
      {

            if(first->adata>second->adata)
            {

                temp=first->adata;
                first->adata=second->adata;
                second->adata=temp;
                first=start->ptr;
                second=first->ptr;
            }
            else
            {
                first=first->ptr;
                second=second->ptr;
            }
      }while(second!=NULL);

      entry=start->ptr;
      while (entry!=NULL)
      {
        cout<<entry->adata<<endl;
        entry=entry->ptr;

      }
      cout<<endl;


      cout<<endl<<"Enter a number you want to search."<<endl;
      cin>>akey;
      entry=start->ptr;
      while ((akey != entry->adata) && (entry->ptr != NULL))
      {
        entry=entry->ptr;
      }
      if (akey==entry->adata)
      {
        cout<<"found"<<endl;
      }
      else
      {
        cout<<"not found"<<endl;
      }
      return 0;
}
从上面的小动画也能看出,我是用C-FREE编译的,没有问题,刚才又用DEV-C++编译并运行了,也没有问题的。




[ 本帖最后由 dubaoshi 于 2008-5-20 13:32 编辑 ]

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-5-20 13:29
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.049999 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved