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

I am given a partial program and need to combine the two lists and output the un

ID: 3814536 • Letter: I

Question

I am given a partial program and need to combine the two lists and output the union. Please help finish the program only edit where the "TO DO" comment is at the end of the program. Thank you.

The implementation will use the C++ Standard Template Library (STL) list implementation. The printList() function provides an example. The listUnion() function will create the union of two given lists (L1 and L2) and will place the results in a third temporary list called tmpList that is returned to the calling function. The union of two lists will consist of all items in both lists.

/**
* List.cpp - This program implements and tests the
* listUnion() function.
*
* TODO: Include your name and course number here.
*/

#include <iostream>
#include <cstdio>
#include <list>

using namespace std;

template <class Object>
void printList(list<Object> L);

template <class Object>
list<Object> listUnion(list<Object> L1, list<Object> L2);

int main(int argc, char **argv)
{
list<int> L1;
list<int> L2;
list<int> resultsList;

// Add some data to list 1
L1.push_back(33);
L1.push_back(89);
L1.push_back(46);
L1.push_back(69);
L1.push_back(81);

// Add some data to list 2
L2.push_back(12);
L2.push_back(21);
L2.push_back(81);
L2.push_back(70);
L2.push_back(75);

// Print out the lists
cout << "List 1:" << endl;
printList(L1);

cout << "List 2:" << endl;
printList(L2);

resultsList = listUnion(L1, L2);

cout << "The union of List 1 with List 2 is:" << endl;
printList(resultsList);

cout << "** Press any key to continue **" << endl;
getchar();

return 0;
}

template <class Object>
void printList(list<Object> L)
{
cout << " ";

// Use an iterator to walk the list. Print the value of
// for each node.
typename list<Object>::iterator itr;
for (itr = L.begin(); itr != L.end(); ++itr)
{
// Get the contents of each node
Object value = (*itr);

// Print the value of the node
cout << " " << value;
}

cout << endl << endl;

return;
}

template <class Object>
list<Object> listUnion(list<Object> L1, list<Object> L2)
{
// Set the temporary list equal to list 1
list<Object> tmpList = L1;
  
for (int i = 0; i < L2.size(); i++)
{
  
}
  
// TODO: Create the union of list 1 and list 2 by inserting
// list 2 into the temporary list.

return tmpList;
}

Explanation / Answer

/**
* List.cpp - This program implements and tests the
* listUnion() function.
*
* TODO: Include your name and course number here.
*/
#include <iostream>
#include <cstdio>
#include <list>
#include <algorithm>

using namespace std;
template <class Object>
void printList(list<Object> L);
template <class Object>
list<Object> listUnion(list<Object> L1, list<Object> L2);
int main(int argc, char **argv)
{
   list<int> L1;
   list<int> L2;
   list<int> resultsList;

   // Add some data to list 1
   L1.push_back(33);
   L1.push_back(89);
   L1.push_back(46);
   L1.push_back(69);
   L1.push_back(81);

   // Add some data to list 2
   L2.push_back(12);
   L2.push_back(21);
   L2.push_back(81);
   L2.push_back(70);
   L2.push_back(75);

   // Print out the lists
   cout << "List 1:" << endl;
   printList(L1);
   cout << "List 2:" << endl;
   printList(L2);
   resultsList = listUnion(L1, L2);

   cout << "The union of List 1 with List 2 is:" << endl;
   printList(resultsList);
   cout << "** Press any key to continue **" << endl;
   getchar();
   return 0;
}
template <class Object>
void printList(list<Object> L)
{
   cout << " ";
   // Use an iterator to walk the list. Print the value of
   // for each node.
   typename list<Object>::iterator itr;
   for (itr = L.begin(); itr != L.end(); ++itr)
   {
       // Get the contents of each node
       Object value = (*itr);
       // Print the value of the node
       cout << " " << value;
   }
   cout << endl << endl;
   return;
}
template <class Object>
list<Object> listUnion(list<Object> L1, list<Object> L2)
{
   // Set the temporary list equal to list 1
   list<Object> tmpList = L1;

   // 33 89 46 69 91

   // 12 21 81 70 75

   int size = L2.size();
   std::list<Object>::iterator findIter;

   for (int i = 0; i < size; i++)
   {
       findIter = std::find(tmpList.begin(), tmpList.end(), L2.front());
       //cout << L2.front();
       if (findIter==tmpList.end()) // element we searched NOT found
       {
           // do insert this element
           tmpList.push_back(L2.front());
           //printList(tmpList);
           //printList(L2);
           // pop element from front of L2
           L2.pop_front();
           //printList(tmpList);
           //printList(L2);

       }
       else // found
       {
           // this element not found till end of out list is reached in tmpList, so insert in tmplist
           L2.pop_front();
           //printList(tmpList);
           //printList(L2);

       }

   }

   // TODO: Create the union of list 1 and list 2 by inserting
   // list 2 into the temporary list.
   return tmpList;
}