11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

136 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** Dictionary implemented by unsorted array-based list. */class UALdictionary implements Dictionary {priv<strong>at</strong>e st<strong>at</strong>ic final int defaultSize = 10; // Default sizepriv<strong>at</strong>e AList list; // To store dictionary/** Constructors */UALdictionary() { this(defaultSize); }UALdictionary(int sz){ list = new AList(sz); }/** Reinitialize */public void clear() { list.clear(); }/** Insert an element: append to list */public void insert(Key k, E e) {KVpair temp = new KVpair(k, e);list.append(temp);}/** Use sequential search to find the element to remove */public E remove(Key k) {E temp = find(k);if (temp != null) list.remove();return temp;}/** Remove the last element */public E removeAny() {if (size() != 0) {list.moveToEnd();list.prev();KVpair e = list.remove();return e.value();}else return null;}/** Find k using sequential search@return Record with key value k */public E find(Key k) {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}Figure 4.33 A dictionary implemented with an unsorted array-based list.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!