The code in lines 15 - 18 is good but there is a problem with the inner for loop.
I tried your sort function on my own list and it crashes when sorting a list when the last 2 elements need to be swapped.
This is because line 17 makes tmp2->next = NULL , then tmp2 is "incremented" in the for loop making tmp2 = NULL and finally the test in the for loop
tmp2->next != NULL
causes the crash.
Inserting these lines after line 20 prevents the crash:
1 2
|
if( !tmp2->next )
break;
|
The Sort() suffers from other problems as well.
1) The head value stays at the head, even if it is > head->next->data.
2) Nodes get lost. Sorting the list of values 9 7 6 4 2 produces 9 2 4 6. The Node with data = 7 gets lost. I think tmp1 is not following just behind tmp2 as expected but I'm not sure.
Hope this helps.
EDIT: Yes, problem 2 is caused by tmp1 falling out of sync with tmp2. Replacing line 20:
tmp1 = tmp1->next;
with
tmp1 = tmp2;
solves problem 2. soring 9 7 6 4 2 produces 9 2 4 6 7 leaving just problem 1.