Write a program that merges two ordered list objects of integers into a single ordered list object of integers. Function merge should receive references to each of the list objects to be merged and reference to a list object into which the merged elements will be placed.
What will be an ideal response?
```
#include
using namespace std;
#include "List.h"
template< typename T >
void merge( List< T > &first, List< T > &second, List< T > &result )
{
List< T > tempFirst( first ); // create a copy of first
List< T > tempSecond( second ); // create a copy of second
T value1;
T value2;
tempFirst.removeFromFront( value1 );
tempSecond.removeFromFront( value2 );
while ( !tempFirst.isEmpty() && !tempSecond.isEmpty() )
{
if ( value1 <= value2 )
{
result.insertAtBack( value1 );
tempFirst.removeFromFront( value1 );
} // end if
else
{
result.insertAtBack( value2 );
tempSecond.removeFromFront( value2 );
} // end else
} // end while
// Insert the values currently in value1 and value2
if ( value1 < value2 )
{
result.insertAtBack( value1 );
result.insertAtBack( value2 );
} // end if
else
{
result.insertAtBack( value2 );
result.insertAtBack( value1 );
} // end else
// Complete the insertion of the list that is not empty.
// NOTE: Only one of the following 2 while statements will execute
// because one of the lists must be empty to exit the preceding while
if ( !tempFirst.isEmpty() ) // items left in tempFirst
{
do
{
tempFirst.removeFromFront( value1 );
result.insertAtBack( value2 );
} while ( !tempFirst.isEmpty() );
} // end if
else // items left in tempSecond
{
do
{
tempSecond.removeFromFront( value2 );
result.insertAtBack( value2 );
} while ( !tempSecond.isEmpty() );
} // end else
} // end function merge
int main()
{
List< int > list1;
List< int > list2;
List< int > result;
int i;
for ( i = 1; i <= 9; i += 2 )
list1.insertAtBack( i );
list1.print();
for ( i = 2; i <= 10; i += 2 )
list2.insertAtBack( i );
list2.print();
merge( list1, list2, result );
cout << "The merged list is:\n";
result.print();
return 0; // indicates successful termination
} // end main
```
The list is: 1 3 5 7 9
The list is: 2 4 6 8 10
All nodes destroyed
All nodes destroyed
The merged list is:
The list is: 1 2 3 4 5 6 7 8 9 10
Destroying nodes ...
1 2 3 4 5 6 7 8 9 10
All nodes destroyed
Destroying nodes ...
2 4 6 8 10
All nodes destroyed
Destroying nodes ...
1 3 5 7 9
All nodes destroyed
You might also like to view...
Examples of End Users in SNA include
a. a Workstation user b. an Application Program c. both a Workstation user and an Application Program d. none of the above
To obtain the first element in a vector v, use _______.
A. v[1]; B. v.at(1); C. v.at(0); D. v[0];
A set of images, drawings, photographs, videos and sound included with Microsoft Office or accessed from Microsoft Office Online is called ________ ________
Fill in the blank(s) with correct word
??In Microsoft Edge, Reading View will not block pop-up ads or other clutter on a webpage. __________________
Answer the following statement true (T) or false (F)