/* * list_dictionary.cpp * * Created on: 23.01.2014 * Author: trifon */ #include using namespace std; #include "dictionary.h" // O(n) template I member(T const& x, List const& l) { I it = l.begin(); while (it && *it != x) ++it; return it; } template class ListDictionary : LinkedList >, public Dictionary { public: typedef LinkedListIterator > I; // O(n) V* search(K const& key) { I it = member(KeyValuePair(key), *this); if (!it) return NULL; return &(*it).value; } // O(1) // O(n), ако има проверка за повторение bool add(K const& key, V const& value) { this->toEnd(KeyValuePair(key,value)); // хубаво е да направим проверка за повторение на ключа return true; } // O(n) bool remove(K const& key, V& value) { I it = member(KeyValuePair(key), *this); if (!it) return false; KeyValuePair kvp; this->deleteAt(kvp, it); value = kvp.value; return true; } // O(n) LinkedList keys() const { LinkedList result; for(I it = this->begin(); it; ++it) result.toEnd((*it).key); return result; } };