注册 登录
编程论坛 C++教室

程式问题

suckdog 发布于 2008-05-17 12:58, 1140 次点击
我这个程式现在要按照从小到大的顺序排应该怎么弄呢? 现在它好像不能按顺序排


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;
}
10 回复
#2
suckdog2008-05-18 09:29
自己顶一下
#3
中学者2008-05-18 09:57
链表排序么????可以直接交换结点值而不修改结点的连接顺序....
#4
Aegisys2008-05-18 13:17
晕,把First和Second两个指针搞了一通,好像链表还是原样...
#5
suckdog2008-05-19 05:19
对,现在是原样,现在谁把那个地方从写一下,我看看哪里不对, 光说不清楚。
#6
dubaoshi2008-05-20 11:47
我周六看了好久,加上今天又调了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);这里把标志给去掉了


累死啦~~~
#7
dubaoshi2008-05-20 11:53
虽然是能运行了,但这个链表我还都不大懂:(
用new建的这个链表在退出程序时,并没有回收内存啊,呵呵~~
#8
suckdog2008-05-20 12:11
你的程式我试了一下,成功一般, 我输入 3,2,1, 它输出 2,3,1, 很明显没有回头继续排列
#9
dubaoshi2008-05-20 13:28

怎么会呢?在我这里是完全正常的啊。
如下动画所示:
只有本站会员才能查看附件,请 登录

有问题再交流啊~
#10
dubaoshi2008-05-20 13:29
完整程序如下:
#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++编译并运行了,也没有问题的。




[[it] 本帖最后由 dubaoshi 于 2008-5-20 13:32 编辑 [/it]]
#11
suckdog2008-05-21 10:16
你的程式我试过了,可以了, 谢谢, 顺便问一下,如何删除一个数字, 比如我先输入1,2,3,4

然后我要把3删掉,该怎么弄呢??
1