Given two ordered lists L1 and L2 , write a function:
void bool_opers(list<int> &Lxor, list<int> &Land, list<int> &L1, list<int> &L2);
The algorithm must generate in Lxor a new sorted list with all the elements that are in only one of the two original lists, and in Land a new list ordered with all the elements that are in both.
For example, if L1 = (1,3,5,7,9) and L2 = (3,4,5,6,7) , then the algorithm must generate the lists
Lxor = (1,4,6,9) and Land = (3,5,7) .
The code you reach is the following:
auto it=L1.begin();
while(it != L1.end()) {
auto it2=L2.begin();
bool flag = false;
while(it2 != L2.end()){
if(*it == *it2) {
Land.push_back(*it);
it = L1.erase(it);
it2 = L2.erase(it2);
--it; --it2;
}
++it2;
}
++it;
}
Lxor.insert(Lxor.begin(), L1.begin(), L1.end());
Lxor.insert(Lxor.end(), L2.begin(), L2.end());
Lxor.sort();
It seems to work in most cases. The problem is that I try this code in an evaluator function that teachers in my algorithm class and data structure use and it goes into an infinite loop. This function is to test the code with many cases and say if it is correct or not. I can not see the cases that are tested so I do not know where I'm failing.