Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. oid MergeSort(Tasks **headRef){
  2. Tasks *head = *headRef;
  3. Tasks *a;
  4. Tasks *b;
  5.  
  6. if ((head == NULL) || (head->Next == NULL))
  7. {
  8. return;
  9. }
  10. Split(head, &a, &b);
  11.  
  12. MergeSort(&a);
  13. MergeSort(&b);
  14.  
  15. *headRef = SortedMerge(a, b);
  16. }
  17.  
  18. Tasks *SortedMerge(Tasks *a, Tasks *b){
  19. Tasks *result = NULL;
  20.  
  21. if (a == NULL)
  22. return(b);
  23. else if (b==NULL)
  24. return(a);
  25.  
  26. if (a->hour < b->hour)
  27. {
  28. result = a;
  29. result->Next = SortedMerge(a->Next, b);
  30. }
  31. else if (a->hour > b->hour)
  32. {
  33. result = b;
  34. result->Next = SortedMerge(a, b->Next);
  35. }
  36. else
  37. {
  38. if (a->minutes < b->minutes)
  39. {
  40. result = a;
  41. result->Next = SortedMerge(a->Next, b);
  42. }
  43. if (a->minutes > b->minutes)
  44. {
  45. result = b;
  46. result->Next = SortedMerge(a, b->Next);
  47. }
  48. }
  49. return(result);
  50. }
  51.  
  52. void Split(Tasks *source, Tasks **frontRef, Tasks **backRef)
  53. {
  54. Tasks *fast;
  55. Tasks *slow;
  56. if (source==NULL || source->Next==NULL)
  57. {
  58. *frontRef = source;
  59. *backRef = NULL;
  60. }
  61. else
  62. {
  63. slow = source;
  64. fast = source->Next;
  65.  
  66. while (fast != NULL)
  67. {
  68. fast = fast->Next;
  69. if (fast != NULL)
  70. {
  71. slow = slow->Next;
  72. fast = fast->Next;
  73. }
  74. }
  75.  
  76. *frontRef = source;
  77. *backRef = slow->Next;
  78. slow->Next = NULL;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement