Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

Which browser does the best job of protecting privacy? Why?

Answered: 1 week ago