Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. void insertion_sort(LinkedList *list, int (*compare)(void *, void *))
  2. {
  3. LinkedList *sortedList = initialise_linked_list();
  4. int compareVal;
  5. if(!list){
  6. return;
  7. }
  8. if(list->head == NULL){
  9. return;
  10. }
  11. append_linked_list(sortedList, list->head->data);
  12. remove_tail_linked_list(list);
  13. while(list->head){
  14. Node *removedHead = sortedList->head;
  15. while(removedHead){
  16. compareVal = (*compare)(list->head->data, removedHead->data);
  17. if(compareVal<0){
  18. prepend_linked_list(sortedList, list->head->data);
  19. remove_head_linked_list(list);
  20. }
  21. removedHead = removedHead->next;
  22. }
  23. if(!removedHead){
  24. append_linked_list(sortedList, list->head->data);
  25. remove_head_linked_list(list);
  26. }
  27. }
  28. list->head = sortedList->head;
  29. list->tail = sortedList->tail;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement