Esempio n. 1
0
/**
 * 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
}
Esempio n. 2
0
/**
 * if l is empty, no need to copy
 * else add each node's values one by one.
 */
void MyLinkedList::_copy(const MyLinkedList &l)
{
	_init();

	if (l._isEmpty())
	{
		return;
	}

	for (MyLinkedListNode *pCurrent = l._pHead; pCurrent != NULL ;
			pCurrent = pCurrent->getNext())
	{
		add(pCurrent->getKey(), pCurrent->getData());
	}
}
Esempio n. 3
0
int main() {
    MyLinkedList<int> list = MyLinkedList<int>();
    list.insert(1);
    list.insert(2);
    list.insert(8);
    list.insert(3);
    list.insert(4);
    list.insert(5);
    list.insert(1);
    list.insert(2);
    list.insert(4);
    list.insert(6);
    list.insert(6);
    list.insert(6);
    list.insert(7);
    list.print_list();
    MyLinkedList<int> *new_list = remove_duplicates(&list);
    new_list->print_list();
    delete new_list;
    new_list = remove_duplicates_2(&list);
    new_list->print_list();
    return 0;
}
Esempio n. 4
0
int main()
{
    using namespace std;

    MyLinkedList *mlst = new MyLinkedList(1,new MyLinkedList(3,nullptr));

    mlst->printList();
    cout << endl << mlst->getElementAt(1) << endl << mlst->length() << endl << endl;

    mlst->insert(2,0);

    mlst->printList();
    cout << endl << mlst->getElementAt(1) << endl << mlst->length() << endl << endl;

    mlst->deleteElementAt(0);

    mlst->printList();
    cout << endl << mlst->getElementAt(1) << endl << mlst->length() << endl << endl;

    //cout << mlst;

    return 0;
}
Esempio n. 5
0
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;
}