Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is a question from C++ Plus Data Structures 5 ed. I am assuming I

ID: 3855053 • Letter: T

Question

The following is a question from C++ Plus Data Structures 5 ed.

I am assuming I start with code from book, but im not sure.

Write a client function that merges two instances of the Sorted List ADT using the following specification.

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 list2.

Write the function definition, using a linked implementation.

In order to verify that the code of MergeLists(SortedType list1, SortedType list2, SortedType& result) works, please integrate your code in the SortedType code in the book pages 239-243.

Explanation / Answer

Datatype i mentioned here is itemType

This may vary accordingly

MergeLists(SortedType list1, SortedType list2, SortedType& result) {
   itemType item1, item2;
   int count1 = 0, count2 = 0;
   list1.ResetList();
   list2.ResetList();
   item1 = list1.GetNextItem();
   count1++;
   item2 = list2.GetNextItem();
   count2++;
   while (count1 < list1.GetLength() && count2 < list2.GetLength()) {
       if (item1.ComparedTo(item2) < 0) {
           result.putItem(item1);
           item1 = list1.GetNextItem();
           count1++;
       }
       else {
           result.putItem(item2);
           item2 = list2.GetNextItem();
           count2++;
       }
   }
   while(count1 < list1.GetLength()){
       result.putItem(item1);
       item1 = list1.GetNextItem();
       count1++;
   }
  
   while(count2 < list2.GetLength()){
       result.putItem(item2);
       item2 = list2.GetNextItem();
       count2++;
   }
}