I am new into coding and I am trying to create a linked list by inserting nodes. I have taken a class Node that defines the structure of the node. I think my code is fine and there are no errors or warnings on compiling, but there is some runtime error. Please check and help.
#include <iostream>
using namespace std;
class Node
{
public:
int data ;
Node *next;
Node *head = NULL;
void Display (Node *p){
while(p!=NULL)
{
cout<< p->data << " ";
p=p->next;
}
}
void InsertAtLast(Node *p , int index , int x)
{
Node *t;
int i;
if(index < 0 )
return;
t = new Node ;
t->data = x;
if(index == 0)
{
t->next = p ;
head = t;
}
else{
for(i=0 ; i<index-1 ; i++)
p=p->next;
t->next = p->next;
p->next = t;
}
}
};
int main()
{
Node *head = NULL;
Node obj;
obj.InsertAtLast(head , 0 , 150);
obj.InsertAtLast(head , 3 , 200);
obj.Display(head);
return 0;
}