Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- oid MergeSort(Tasks **headRef){
- Tasks *head = *headRef;
- Tasks *a;
- Tasks *b;
- if ((head == NULL) || (head->Next == NULL))
- {
- return;
- }
- Split(head, &a, &b);
- MergeSort(&a);
- MergeSort(&b);
- *headRef = SortedMerge(a, b);
- }
- Tasks *SortedMerge(Tasks *a, Tasks *b){
- Tasks *result = NULL;
- if (a == NULL)
- return(b);
- else if (b==NULL)
- return(a);
- if (a->hour < b->hour)
- {
- result = a;
- result->Next = SortedMerge(a->Next, b);
- }
- else if (a->hour > b->hour)
- {
- result = b;
- result->Next = SortedMerge(a, b->Next);
- }
- else
- {
- if (a->minutes < b->minutes)
- {
- result = a;
- result->Next = SortedMerge(a->Next, b);
- }
- if (a->minutes > b->minutes)
- {
- result = b;
- result->Next = SortedMerge(a, b->Next);
- }
- }
- return(result);
- }
- void Split(Tasks *source, Tasks **frontRef, Tasks **backRef)
- {
- Tasks *fast;
- Tasks *slow;
- if (source==NULL || source->Next==NULL)
- {
- *frontRef = source;
- *backRef = NULL;
- }
- else
- {
- slow = source;
- fast = source->Next;
- while (fast != NULL)
- {
- fast = fast->Next;
- if (fast != NULL)
- {
- slow = slow->Next;
- fast = fast->Next;
- }
- }
- *frontRef = source;
- *backRef = slow->Next;
- slow->Next = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement