Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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* unorderedLinkedList::mergeList(nodeType* first1,nodeType* first2)

{ nodeType *lastSmall; //pointer to the last node of

//the merged list

nodeType *newHead; //pointer to the merged list

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::recMergeSort(nodeType* &head)

{

nodeType *otherHead;

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::mergeSort() {

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago