When entering value in head node it works but when creating mid nodes it gives segmentation fault. What changes should i make?
Tried making various changes also tried to remove segmentation fault by looking upon NULL instances but failed. Beg your pardon if the question seems silly but i tried really hard.
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
};
node *head,*pre,*curr,*tail;
void createhead(int roll)
{
head->data=roll; //creating head
head->next=NULL;
pre=head;
}
void createlist(int roll)
{
curr->data=roll; //creating new node
curr->next=NULL; //in this two lines.
pre->next=curr;
pre=curr;
}
void disp()
{
curr=head;
while(curr!=NULL)
{
cout<<"Value---- \t"<<curr->data<<"\n";
curr=curr->next;
}
}
int main()
{
head =new node();
pre=new node();
tail=new node();
cout<<"Enter 999 to stop\n";
int roll;
cout<<"enter roll number\n";
cin>>roll;
if(roll!=999)
createhead(roll);
for(;;)
{
cout<<"enter roll number\n";
cin>>roll;
if(roll==999)break;
createlist(roll);
}
disp();
}
Expected to create a complete linked list.