Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- using namespace std;
- int main() {
- list<int> L1;
- list<int> L2;
- L1.push_back(1);
- L1.push_back(3);
- L1.push_back(8);
- L1.push_back(12);
- L2.push_back(3);
- L2.push_back(4);
- L2.push_back(8);
- listIntersection(L1, L2);
- system("pause");
- return 0;
- }
- // ALTHOUGH IT'S A TEMPLATE, I'M JUST USING INTS, SO I CAN USE < > etc.
- template<typename T>
- list<T> listIntersection(const list<T>& L1, const list<T>& L2) {
- if ( L1.empty() && L2.empty() ) {
- cout << "Returning an empty list because the two arugment lists were empty\n\n";
- list<int> L3;
- return L3;
- }
- list<T>::const_iterator itr1 = L1.begin();
- list<T>::const_iterator itr2 = L2.begin();
- // Create New List
- list<int> L3;
- for (itr1; itr1 != L1.end(); itr1++) {
- while ( *itr2 < *itr1 ) {
- itr2++;
- }
- if ( (*itr1 == *itr2) && (itr2 != L2.end()) ) {
- int tmp = *itr1;
- L3.push_back(tmp);
- }
- }
- return L3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement