So i am using mingwx64 compiler and vscode editor but display function is not working properly but when i try to display in main() function with same logic as arr.display() It works. I tried running the same code on onlinegdb website and it was running as intended. Is is a gcc bug or a feature i am not aware of ?
#include <iostream>
using namespace std;
class nodee
{
public:
int data;
nodee *next;
};
class ll
{
public:
nodee **head = NULL;
nodee **push(int no = 1)
{
nodee *node;
int n;
if (head != NULL)
{
node = *head;
while ((*node).next != NULL)
{
node = (*node).next;
}
while (no--)
{
nodee *new_node = new nodee();
cout << "Enter the element : ";
cin >> n;
(*node).next = new_node;
new_node->data = n;
new_node->next = NULL;
node = (*node).next;
}
}
else
{
nodee *first = new nodee();
head = &first;
node = *head;
cout << "Enter the element : ";
cin >> n;
first->data = n;
no--;
while (no--)
{
nodee *new_node = new nodee();
cout << "Enter the element : ";
cin >> n;
(*node).next = new_node;
new_node->data = n;
new_node->next = NULL;
node = (*node).next;
}
}
return head;
}
void display()
{
nodee *node = *head;
while (node->next != NULL)
{
cout << node->data<<endl;
node = node->next;
}
cout<<node->data<<endl;
}
};
int main()
{
ll arr;
arr.push(5);
arr.display();
return 0;
}
CodePudding user response:
The error is head = &first;
, you are using a pointer to a local variable as head
, which is invalid after the push
function.
The solution is to make head
only be a nodee *
, you do not need the extra *
:
#include <iostream>
using namespace std;
class nodee
{
public:
int data;
nodee *next;
};
class ll
{
public:
nodee *head = NULL;
nodee *push(int no = 1)
{
nodee *node;
int n;
if (head != NULL)
{
node = head;
while ((*node).next != NULL)
{
node = (*node).next;
}
while (no--)
{
nodee *new_node = new nodee();
cout << "Enter the element : ";
cin >> n;
(*node).next = new_node;
new_node->data = n;
new_node->next = NULL;
node = (*node).next;
}
}
else
{
nodee *first = new nodee();
head = first;
node = head;
cout << "Enter the element : ";
cin >> n;
first->data = n;
no--;
while (no--)
{
nodee *new_node = new nodee();
cout << "Enter the element : ";
cin >> n;
(*node).next = new_node;
new_node->data = n;
new_node->next = NULL;
node = (*node).next;
}
}
return head;
}
void display()
{
nodee *node = head;
while (node->next != NULL)
{
cout << node->data<<endl;
node = node->next;
}
cout<<node->data<<endl;
}
};
int main()
{
ll arr;
arr.push(5);
arr.display();
return 0;
}
not working: https://godbolt.org/z/cr4rY4oKK
working: https://godbolt.org/z/88rd53jGP
CodePudding user response:
Your declaration of the list does not make a sense.
Instead of declaring the data member head
like
nodee *head = NULL;
you declared it like
nodee **head = NULL;
Within the function push
when it is called the first time the pointer head
is set by the address of the local variable first
that will not be alive after exiting the function
nodee *first = new nodee();
head = &first;
So the pointer head
will be invalid.
Also the function push
has a confusing logic. What is should do is to push an integer value on the list. So its parameter should specify the value that will be pushed not the number of nodes that will be pushed.
That is the function should be declared like
void push( int data );
Pay attention to that if you have a one-sided singly-linked list then new nodes should be appended to the beginning of the list. Otherwise you need to define a twos-ded singly-linked list.