0

i have written following insertion sort program for a doubly linked list completely by myself.The the sort function is giving me Segmentation error and i am unable to figure it out...Plz help me out and forgive me if the code is too wrong to handle..

```
//insertion sort using doubly linked list
//the sort function is taking &head as argument.
//if anyone has a simpler algo,please let me know
void sort(node **q)
{
int n=0;
node *cur;
cur=*q;
if (cur->next==NULL) //there is only one element in list
return;
node *end,*tmp;
while(cur->next!=NULL)
cur=cur->next; //reach the last node
while(cur->prev!=NULL)
{
end=cur;
tmp=cur->prev;
cur=cur->prev;
while(tmp!=NULL && tmp->data>=end->data) //comparison starts here
{
tmp=tmp->prev;
n++;
}
if (n)
{
if (tmp==NULL) //currently compared node is smallest
{
tmp=*q;
end->prev=NULL;
end->next=tmp;
end->next->prev=end;
*q=end;
}
else
{ //currently compared node should lie in between somewhere in sorted list
tmp->prev->next=end;
end->prev=tmp->prev;
tmp->prev=end;
end->next=tmp;
}
cur->next=NULL; //making the next pointer on node previous to currently compared node as NULL
}
}
}
```