I am trying to implement a linked list using following code, I got segment fault error please tell me where is the problem.
I’m using ubuntu gcc compiler, please suggest me something
#include<iostream>
using std::cout;
using std::cin;
class ll
{
struct node
{
int info;
node *nextnode ;
}*n;
public:
ll()
{
n=NULL;
}
void getinfo()
{
node *temp,*r;
if( n==NULL )
{
temp=new node;
cout<<" \n enter the first elements of linklist \n";
int z;
cin>>z;
//i guess problem starts here
temp->info=z;
cout<<"the value of info is";
temp->nextnode = NULL;
n=temp;
}
else{
temp=n;
cout<<"heheh balls";
while(temp->nextnode==NULL)
{
temp=temp->nextnode;
}
r=new node;
cout<<"enter the element \t";
int y;
cin>>y;
r->info=y;
r->nextnode=NULL;
temp=r;
}
}
void display()
{
node *temp=n;
while(temp->nextnode==NULL)
{
cout<<temp->info;
}
}
};
int main()
{
ll p;
int v;
cout<<"enter the number of elements to be added to linklist \t";
cin>>v;
//tryn to input linklist from terminal
for(int i=0;i<v;i++)
{
p.getinfo();
}
p.display();
return 0;
}
should be
Same goes for:
should be: