Segmentation fault seems to be coming

#include
using namespace std;

class node
{
public:
node *prev;
int data;
node *next;

node(int d)
{
    prev=NULL; 
    data=d;
    next=NULL;
}

};

node *head=NULL;

void Display(node *p)
{
while(p!=NULL)
{
cout<data<<" ";
p=p->next;
}
cout<<endl;
}

void Create(int n)
{
int ele;
cin>>ele;
head=new node(ele);
node *last=head;

for(int i=0;i<n-1;i++)
{
    cin>>ele;
    node *t=new node(ele);
    t->prev=last;
    last->next=t;
    last=t;
}

}

void InsertionSort(int n)
{
node *p=head;
node *q=head->next;
for(int i=1;i<n;i++)
{
int x=q->data;
while(p->data>x && p!=NULL)
{
p->next->data=p->data;
p=p->prev;
}
if(q->prev!=head)
{
p=head;
}

    else
       p=q->prev;
    
    p->data=x;
    q=q->next;
    p=q->prev;
}

}

int main()
{
int n;
cin>>n;

Create(n);
InsertionSort(n);
Display(head);

}

hi @Mukul-Shane-1247687648773500, kindly share the code on ide.codingblocks .com big code is difficult to read here

unable to load anything on ide.codingblocks.com. too much delay on site

I have submitted the code here.please check.

Sir I haven’t received any update from you since 5 hours on this doubt I posted.i have already submitted the code here.Please check it.

hi @Mukul-Shane-1247687648773500,
sorry i was little busy
the code is not able to handle null pointer
you are doing int x=q->data;
but q becomes null just dry run for 2 nodes say 2 and 3
also its not retaining the node value (2) in this case

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.