In the TODO section: Implement the listIntersection so that it returns the inter
ID: 3637292 • Letter: I
Question
In the TODO section: Implement the listIntersection so that it returns the intersection or set of common items in list1 and list2 and places these items in the list called intersection.#include <iostream>
#include "linkedList.h"
using namespace std;
template <class Type>
void listDifference(linkedListType<Type>& difference,
linkedListType<Type>& list1,
linkedListType<Type>& list2);
template <class Type>
void listIntersection(linkedListType<Type>& intersection,
linkedListType<Type>& list1,
linkedListType<Type>& list2);
int main(int argc, char *argv)
{
// Declare list variables
linkedListType<int> list1;
linkedListType<int> list2;
linkedListType<int> difference;
linkedListType<int> intersection;
// Add some data to list 1
list1.insertLast(42);
list1.insertLast(32);
list1.insertLast(54);
list1.insertLast(58);
list1.insertLast(96);
list1.insertLast(17);
// Add some data to list 2
list2.insertLast(17);
list2.insertLast(31);
list2.insertLast(42);
list2.insertLast(32);
list2.insertLast(3);
list2.insertLast(13);
cout << " List 1: ";
list1.print();
cout << " List 2: ";
list2.print();
listDifference(difference, list1, list2);
cout << " List 1 and List 2 difference: ";
difference.print();
cout << " List 1 and List 2 intersection: ";
listIntersection(intersection, list1, list2);
intersection.print();
cout << " ** Press any key to continue ** ";
getchar();
return 0;
}
template <class Type>
void listDifference(linkedListType<Type>& difference,
linkedListType<Type>& list1,
linkedListType<Type>& list2)
{
linkedListIterator<int> itr1;
linkedListIterator<int> itr2;
for (itr1 = list1.begin(); itr1 != list1.end(); ++itr1)
{
// Add the each element in list 1 to the difference list
difference.insertLast(*itr1);
for (itr2 = list2.begin(); itr2 != list2.end(); ++itr2)
{
if (*itr1 == *itr2)
{
// If the node is in both lists delete the node from the
// difference list.
difference.deleteNode(*itr1);
}
}
}
return;
}
template <class Type>
void listIntersection(linkedListType<Type>& intersection,
linkedListType<Type>& list1,
linkedListType<Type>& list2)
{
// TODO: Implement the listIntersection so that it returns the
// intersection or set of common items in list1 and list2 and
// places these items in the list called intersection.
//
// Sugguestions: Use two for-loop in a similar way as the
// listDifference() function. If item found in list1 is in
// (or equal) to an item in list2
// then add the item to the intersection list.
return;
}