c++ - 遵循奇怪显示模式的单向链表

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

如果我制作 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;
        }
    }


}

请原谅,因为我已经尽力找到解决方案,所以如果有什么事情很明显的话请原谅。

最佳答案

我看到的问题:

  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') 下的每个 block 中各一行。

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

    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 block 。但是,如您所见,那里不需要它。

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

关于c++ - 遵循奇怪显示模式的单向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379556/

相关文章:

c++ - gcc 看不到 SDL

c++ - CMake 和 Flex/Bison

c - 从哈希表 C 中获取链表

arrays - Perl:使用 'splice()' VS 链表插入数组的性能

对相同类类型的 C++ 引用公开私有(private)成员

c++ - boolean 逻辑问题

C++ : Segmentation fault (core dumped) On linux OS

c - 有错误显示为 "comparison between pointer and integer (' int' 和 'const char *' )"

c - 我的代码在第一次输入 scanf 后停止运行

c - 两个链表的合并功能不起作用