I am working on a university assignment. I am trying to write a sorting of linked lists in C. I am not allowed to change values ββ- only pointers.
Here is my sort function:
struct node *sort_list(struct node *head) { bool swapped ; struct node *cur = head, *first ; if ( head == NULL || head->next == NULL ) return head ; else { do { swapped = false ; while ( cur != NULL && cur->next != NULL ){ if (cur->value > cur->next->value){ cur = swap_with_next( cur ) ; swapped = true ; } cur = cur->next ; } } while (swapped == true) ; } return head ; }
And the swap function:
struct node *swap_with_next(struct node *n) { struct node *tmp ; tmp = n ; n = n->next ; tmp->next = n->next ; n->next = tmp ; return n ; }
Problem: wrong conclusion:
input: 5->2->NULL output: 5->NULL input: 9->1->5->2->8->3 output: 9->5->8
Any help would be greatly appreciated!
source share