Question
You are given a function mergeList() within the unorderedLinkedList header file to merge two sorted sublists. Provide the correct code implementations for the given blank
You are given a function mergeList() within the unorderedLinkedList header file to merge two sorted sublists. Provide the correct code implementations for the given blank code spaces. The final part after these implementations would be to implement a recursive merge sort function which utilizes both of these functions divideList() and mergeList(). The last function implementation would be for mergeSort(). mergeSort() function has to be a public member function of the unorderedLinkedList. Functions of divideList(), mereList() and recMergeSort() will be used to implement our mergeSort() function. We should write the missing codes where it says "COMMENT"
template
{ nodeType
//the merged list
nodeType
if (first1 == NULL) //the first sublist is empty
return first2;
else if (first2 == NULL) //the second sublist is empty
return first1;
else
{ if (/* Compare the first nodes here*/ ) COMMENT
{ /* add the code snippet in order to declare the newHead, first1 node and
lastSmall node into newhead*/COMMENT } else
{ newHead = first2;
first2 = first2->link;
lastSmall = newHead; }
while (first1 != NULL && first2 != NULL)
{ if (first1->info < first2->info){
lastSmall->link = first1;
lastSmall = lastSmall->link;
first1 = first1->link; }
else{
lastSmall->link = first2;
lastSmall = lastSmall->link;
first2 = first2->link;
}
} //end while
if (first1 == NULL) //first sublist is exhausted first
lastSmall->link = first2;
else //second sublist is exhausted first
lastSmall->link = first1;
return newHead; }
}//end mergeList
template
void unorderedLinkedList
{
nodeType
if (head != NULL) //if the list is not empty
if (head->link != NULL)
{ /* If the list has more than one node, divide the list by the head and the other head, after division is complete, you will call the code recursively in order to implement recMergeSort over heads lastly, initialize the hed by calling the mergeList() function */COMMENT }
} //end recMergeSort
template
void unorderedLinkedList
recMergeSort(first);
if (first == NULL)
last = NULL;
else
{ last = first;
while (last->link != NULL)
last = last->link; }
} //end mergeSort
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started