sorting link list

This is a sorting function for double link list but there is always an exception error , what is the problem with this code ?? I need help , i have changed pointers in 6 different places but i always get an exception error in the 6th part

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sort()
{
node *temp=first;
while (temp!=NULL)
{
	if(temp->number>temp->next->number)
	{
	temp->prev->next=temp->next->next;//1st
	temp->next->prev=temp->prev;//2nd
	temp->prev=temp->next;//3rd 
	temp->next=temp->next->next;//4th
	temp->prev->next=temp;//5th
	temp->next->prev=temp;//6th
	}
	temp=temp->next;

}
}
Line 8 looks like it goes too far, shouldn't temp->prev->next = temp->next;

Line 13 becomes a problem when temp becomes the last node in the list, basically calling NULL->prev. That would be your segmentation fault.

Also, if there is a swap that happens at the beginning of the list, you need to make sure to update first

This sort method doesn't completely sort, it's like one iteration of a bubble sort.
Topic archived. No new replies allowed.