1
#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;
}
};

Insertion is working fine(addele func is fine)

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

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

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

The problem is, although my code is right for the display function but I am getting infinte zeros after running the code

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. -Basically, if i insert any number for eg. 1 2 in linked list

  2. -The desired output after calling display function should be 2 1

  3. -But the program ends in never ending program with infinite zeros Can any one help me with this ? I m attaching the output also... I m entering 1, 2 by calling addele function And then, I m getting this after calling display function

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Dipesh Saili
  • 68
  • 12
  • 1
    Use a debugger to trace the program execution. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). And please note that C and C++ are different languages - use only the tag for the language you are actually coding in. – kaylum Nov 08 '20 at 11:19
  • Your code is not C. Please only add language tags related to the language you are using. Also please do not add pictures of plain text output. Instead copy&paste it as plain text. – Gerhardh Nov 08 '20 at 11:20
  • 1
    `curr->next=&n;` you should consult your text book regarding lifetime of local variables. They will not be valid after the function is left. You must allocate dynamic memory for new nodes. – Gerhardh Nov 08 '20 at 11:22
  • 2
    `curr->next=&n;` that results in Undefined Behaviour. Because `n` is a local variable which no longer exists as soon as the function exits. – kaylum Nov 08 '20 at 11:22
  • Hey, Thanks but what should i replace it with? @kaylum – Dipesh Saili Nov 08 '20 at 11:29
  • Same, but what should i replace it with? @Gerhardh – Dipesh Saili Nov 08 '20 at 11:29
  • 1
    In C you use `malloc` while in C++ you should probably use `new` to allocate dynamic memory. And of course you need to free the memory lateron. Any tutorial on linked lists should show you how to do it. – Gerhardh Nov 08 '20 at 11:30
  • So, basically we can't implement a user-defined linked list without dynamic allocation ? .. Is that, So@Gerhardh – Dipesh Saili Nov 08 '20 at 11:47
  • 1
    @DipeshSaili That's it, you cannot implement a dynamic data structure like a linked list without allocation of some sort. Nice attempt though. – john Nov 08 '20 at 11:55

0 Answers0