The following code is my attempt of reordering a linked list. I make a tempHead pointer to the list head and set the list head to null I then iterate thru the list to find the smallest node and add to the head of actual list head. I am getting a segmentation fault: core dumped when I use the while(tempHead!=NULL) if I dont use this code only one node is added to the head of list. It may be because I am overiding bounds somewhere when assigning next, not sure.
Any input or suggestions appreciated
Thanks
Code:
/*
void listReorder (List *pcl)
{
ListNodePtr current,previous,tempHead,smallest;
tempHead=pcl->head;
pcl->head=NULL;
current=tempHead;
smallest=current;
previous=NULL;
while(tempHead!=NULL)
{
while(current!=NULL&&pcl->CompareFPtr(&smallest->data,¤t->data)>0)/*while current not smaller*/
{
previous=current;
current=current->next;
}/*end while*/
if(previous==NULL)/*unlink smallest node*/
{
tempHead=current->next;
}
else /*unlink smallest node*/
{
previous->next=current->next;
}
/*add smallest node to the actual head of list*/
smallest=current;
smallest->next=pcl->head;
pcl->head=smallest;
current=current->next;/*increment current*/
}/*end while*/
}
*/