Write a client function that merges two instances of the Sorted List ADT using t
ID: 3844051 • Letter: W
Question
Write a client function that merges two instances of the Sorted List ADT using the following specification (listed below the questions). 4. Write the function definition, using a linked implementation. 5. Rewrite Exercise 4, making MergeLists an array-based member function of the Sorted List ADT. 6. Rewrite Exercise 5, making MergeLists a linked member function of the Sorted List ADT. Specifications: MergeLists(SortedType list1, SortedType list2, SortedType& result) Function: Merge two sorted lists into a third sorted list. Preconditions: list1 and list2 have been initialized and are sorted by key using function ComparedTo. list1 and list2 do not have any keys in common Postconditions: result is a sorted list that contains all of the items from list1 and list2Explanation / Answer
//use below c++ code to merge two sorted list
void Merge(list<int> &list1, list<int> &list2, list<int> &result)
{
while((!list1.empty()) && (!list2.empty))
{
if(list1.front() < list2.front())
{
result.pushBack(list1.front());
list1.popFront();
}
else
{
result.pushBack(list2.front());
list2.popFront();
}
}
while(!list1.empty())
{
result.pushBack(list2.front());
list2.popFront();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.