Why am i getting segmentation fault while deleting the linked list using the deleteend() function twice?

#include
using namespace std;
//class node for storing elements:-
class node
{
public:
int data;
node* next;
node(int d)
{
data=d;
next=NULL;
}
};

void insert_start(node*&head,int d)
{
if(head==NULL)
{
head=new node(d);
return;
}
node* n=new node(d);
n->next=head;
head=n;

}

void print(node* head)
{
while (head!=NULL)
{
cout<data<<" ";
head=head->next;
}

}

int length(node* head)
{
int cnt=0;
while (head!=NULL)
{
cnt++;
head=head->next;
}
return cnt;
}

void inserttail(node*&head,int data)
{
if (head==NULL)
{
head= new node(data);
return;
}

node*tail=head;
while (tail->next!=NULL)
{
    tail=tail->next;

}
tail->next=new node(data);
return;

}
void insertmiddle(node*&head,int data,int p)
{
if(head==NULL || p==0)
{
insert_start(head,data);
}
else if(p>length(head))
{
inserttail(head,data);
}
else{
int jump=1;
nodetemp=head;
while(jump<=p-1)
{
temp=temp->next;
jump++;
}
node
n=new node(data);
n->next=temp->next;
temp->next=n;
}
}
void deletehead(node*&head)
{
if (head==NULL)
{
return;
}
node*temp=head;
head=head->next;
delete temp;
}

void deletemiddle(node*&head,int p)
{
node* pos=head;
node* prepos=head;
node* mid=head;
for (int i=0;i<p-1;i++)
{
prepos=pos;
pos=pos->next;
mid=pos;

}

pos=pos->next;
prepos->next=pos;
delete mid;
return;

}

int main()
{
node*head=NULL;

insert_start(head,5);

insert_start(head,3);
insert_start(head,2);
insert_start(head,1);
insertmiddle(head,4,3);
inserttail(head,6);

deletehead(head);
deletemiddle(head,2);
deletemiddle(head,6);
print(head);

}