Question: The intersection of two sets consists of only those elements in the first set that are also in the second set. We will use this

The intersection of two sets consists of only those elements in the first set that are also in the second set. We will use this definition for to implement the intersection for two lists. Modify the List.cpp file and implement details for the intersection() function. The prototype for this function is as follows:

template list intersection(list L1, list L2);

The implementation will use the C++ Standard Template Library list implementation. The print() function provides an example on how to use an iterator for a given list. The result of the intersection() function will be the elements in the intersection of the two lists. If there are no common elements between the two given lists, then function will return NULL.

The code for list.cpp is below

#include #include #include

using namespace std;

template void print(list L1);

template list intersection(list L1, list L2);

int main(int argc, char **argv) { // Declare list variables list L1; list L2; list L3;

// Add data to list 1 L1.push_back(39); L1.push_back(81); L1.push_back(66); L1.push_back(65); L1.push_back(96);

// Add data to list 2 L2.push_back(81); L2.push_back(92); L2.push_back(32); L2.push_back(11); L2.push_back(12); L2.push_back(21); L2.push_back(34); L2.push_back(78); L2.push_back(65); L2.push_back(83);

// Add data to list 3 L3.push_back(78); L3.push_back(96); L3.push_back(92); L3.push_back(66); L3.push_back(34); L3.push_back(98); L3.push_back(62);

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

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

cout << "List 3: " << endl; print(L3);

cout << endl;

cout << "The intersection of L2 and L2: " << endl; print(intersection(L2, L2));

cout << "The intersection of L1 and L2: " << endl; print(intersection(L1, L2));

cout << "The intersection of L2 and L3: " << endl; print(intersection(L2, L3));

cout << "The intersection of L3 and L1: " << endl; print(intersection(L3, L1));

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

return 0; }

template void print(list L1) { cout << " ";

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

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

cout << endl;

return; }

template list intersection(list L1, list L2) { // Declare a result list to return to the caller list resultList;

// TODO: Implement the details of the intersection() function // here. return resultList; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!