// From the software distribution accompanying the textbook // "A Practical Introduction to Data Structures and Algorithm Analysis, // Third Edition (C++)" by Clifford A. Shaffer. // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer. #include "dictionary.h" #include "KVpair.h" // Dictionary implemented with an unsorted array-based list template class UALdict : public Dictionary { private: AList >* list; public: UALdict(int size=defaultSize) // Constructor { list = new AList >(size); } ~UALdict() { delete list; } // Destructor void clear() { list->clear(); } // Reinitialize // Insert an element: append to list void insert(const Key&k, const E& e) { KVpair temp(k, e); list->append(temp); } // Use sequential search to find the element to remove E remove(const Key& k) { E temp = find(k); // "find" will set list position if(temp != NULL) list->remove(); return temp; } E removeAny() { // Remove the last element Assert(size() != 0, "Dictionary is empty"); list->moveToEnd(); list->prev(); KVpair e = list->remove(); return e.value(); } // Find "k" using sequential search E find(const Key& k) const { for(list->moveToStart(); list->currPos() < list->length(); list->next()) { KVpair temp = list->getValue(); if (k == temp.key()) return temp.value(); } return NULL; // "k" does not appear in dictionary } int size() // Return list size { return list->length(); } };