错误:显示链接列表而不进行动态分配DISPLAY无法正常工作

问题描述

#include <iostream>
using namespace std;
struct node
{
int a;
node *next=NULL;
 void insert()
{
   cout<<"\n"<<"insert a value ";
    cin>>a; 
}
 void show()
{
    cout<<"value inserted is ";
    cout<<a;
}
};

插入工作正常(addele func正常)

void addele(node *head)
{
node *curr = head;
node n;

while(!(curr->next==NULL))  
{
    curr=curr->next;  
}
curr->next=&n;

n.insert();
n.show();
}

问题是,尽管我的代码适合显示功能,但运行代码后却得到了无限个零

void display(node *head)
{
node *curr = head;

while(!(curr->next==NULL))  
{
    cout<<curr->a;
    curr=curr->next;  
}
}
int main()
{   
int ch;
node n,*h;
h=&n;
while (1)
{
    cout<<" 1. Add Element \n";
    cout<<" 2. display list \n";
    cout<<" 3. Exit \n";
    cin>>ch;

    switch (ch)
    {
    case 1:addele(h);
        break;
    case 2:display(h);
        break;
    }
    if (ch==3)
    {
        return 0;
    }
}
return 0 ;
}
  1. -基本上,如果我插入任何数字,例如。链表中有1 2

  2. -调用显示函数后所需的输出应为2 1

  3. -但是程序以永无穷尽的零结束 谁能帮我这个 ? 我也在附加输出...

    I m entering 1,2 by calling addele function

    And then,I m getting this after calling display function

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)