【问题标题】:Singly linked list following a weird display pattern单链表遵循奇怪的显示模式
【发布时间】:2014-06-16 06:29:03
【问题描述】:

如果我创建 1 个节点并显示,则以下代码可以完美运行。但是,如果我插入 2 个或更多节点,则只显示最后输入的节点以及已经在链表中的节点。例如,如果我链接了 3 1 2 4 的列表,并且我连续输入 2 1 3 并调用显示函数,则输出 = 3 1 2 4 3。

    struct node
{
    char info;
    node* link;
}*f,*nn,*p,*c;//variables to make new node, head and control previous and current

void create(char inf)
{

    if(f=='\0')
    {
        nn=new node[sizeof(struct node)];
        nn->info=inf;
        nn->link='\0';
        f=c=p=nn;
    }
    else
    {

        nn=new node[sizeof(struct node)];
        nn->info=inf;
        p->link=nn;
        c=nn;
        c->link='\0';
    }
}

void display()
{

    c=p=f;
    while(c!='\0')
    {
        cout<<c->info<<" ";
        p=c;
        c=c->link;
    }
    cout<<endl;

}

int main()
{
    while(3)
    {
        int sw=0;
        cout<<"Enter \n1. to create list \n2. to display list"<<endl;
        cin>>sw;
        switch(sw)
        {
            case 1:{
            char info;
            cout<<"Enter info!"<<endl;
            cin>>info;
            create(info);
            break;
            }
            case 2:display();
            break;
            default:
            cout<<"Wrong entry, try again!"<<endl;
        }
    }


}

请原谅,因为我已尽力找到解决方案。

【问题讨论】:

  • 给你的变量起一个有意义的名字,你就会知道你在哪里犯了错误。 fcpnn 不是你想要的。
  • c-&gt;link='\0' 不是一个好主意。最好使用c-&gt;link = 0c-&gt;link = NULL。 c->link 是一个指针,'\0' 是 c++ 中的一个字符。

标签: c++ pointers linked-list nodes singly-linked-list


【解决方案1】:

我看到的问题:

  1. fnnpc 未初始化。

    将行改为:

    struct node
    {
        char info;
        node* link;
    };
    
    node *f = NULL;
    node* nn = NULL;
    node* p = NULL;
    node* c = NULL;
    
  2. 换行

    if(f=='\0')
    

    if (f == NULL)
    

    这是一种风格变化,但更具可读性。仅使用'\0' 比较字符。使用NULL 比较指针。

  3. 下面这行似乎不对。

    nn=new node[sizeof(struct node)];
    

    它分配一个包含sizeof(struct node) 项的对象数组并返回一个指向该数组的指针。你只需要一个对象。将行更改为:

    nn=new node;
    

    有两行这样的行,在if (f=='\0') 下的每个块中都有一行。

  4. 您没有在else 块下正确创建链接。

    nn=new node[sizeof(struct node)];
    nn->info=inf;
    p->link=nn;  // This is good only for the second item.
                 // When you add the third item, the second item becomes an orphan.
                 // When you add the fourth item, the third item becomes an orphan.
    
    c=nn;
    c->link='\0';
    

    你需要的是:

    nn=new node;
    nn->info=inf;
    nn->link = NULL;
    c->next = nn;
    c = nn;
    
  5. 您正在修改display 中的全局变量pc。如果您在调用display 后尝试添加更多项目,您将得到意外的行为。在display 中使用局部变量可以避免这个问题。

    void display()
    {
        Node* n = f;
        while( n != NULL)
        {
            cout << n->info << " ";
            n = n->link;
        }
        cout << endl;
    }
    

建议清理

您不需要全局范围内的变量nn。它仅用于create。将其从全局范围中移除并放入create

您根本不需要全局变量p。您认为它有用的唯一地方是else 块。但是,如您所见,那里不需要它。

使用NULL 而不是'\0' 来比较指针。

【讨论】:

    【解决方案2】:

    问题出在这一行:

          p->link=nn;
    

    您在同一位置添加新节点,而不是更新“p”。因此,如果添加 2 个节点,则在第一个节点所在的位置添加第二个节点,而第一个节点将丢失。因此,您始终只能看到添加的最后一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2013-06-27
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多