/** * This method acts acordingly by the chosen operation op: * MINUS: start with coping this (left) and remove each node in other that has * same key as this. * * UNION: start with coping this (left) and add * each node in other that a different key than this. * * INTERSECTION: start with an empty list adding each node * that has same key as this, with this's data. * * NOTE: The input lists (this instance and other) should not have duplicate * keys, otherwise the output is undefined. * */ MyLinkedList MyLinkedList::_operatorChoice(const MyLinkedList& other, Operation op) const { MyLinkedList result; if(op != INTERSECTION) { result = *this; // O(m) } for (MyLinkedListNode *pCurrent = other._pHead; pCurrent != NULL ; pCurrent = pCurrent->getNext()) { MyLinkedListNode * pFound = _findInList(pCurrent->getKey()); if (pFound == NULL) { if(op == UNION) // union operation is O(m*(m+n)) { result.add(pCurrent->getKey(), pCurrent->getData()); // O(1) } continue; } if (op == MINUS) // minus operation is O(m*(m + n)) { result.remove(pFound->getKey()); } else if (op == INTERSECTION) // and operation is O(m*(m + n)) { result.add(pFound->getKey(), pFound->getData()); } } return result; // copy ctor }
int main() { MyLinkedList<int> list = MyLinkedList<int>(); list.insert(1); list.insert(1); list.insert(2); list.insert(3); list.insert(4); list.insert(5); list.search(3); list.print_list(); list.remove(3); list.print_list(); list.remove(3); list.print_list(); list.remove(5); list.print_list(); list.search(1); list.search(3); list.search(6); list.search(2); return 0; }