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.

132 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** The Dictionary abstract class. */public interface Dictionary {/** Reinitialize dictionary */public void clear();/** Insert a record@param k The key for the record being inserted.@param e The record being inserted. */public void insert(Key k, E e);/** Remove <strong>and</strong> return a record.@param k The key of the record to be removed.@return A maching record. If multiple records m<strong>at</strong>ch"k", remove an arbitrary one. Return null if no recordwith key "k" exists. */public E remove(Key k);/** Remove <strong>and</strong> return an arbitrary record from dictionary.@return the record removed, or null if none exists. */public E removeAny();/** @return A record m<strong>at</strong>ching "k" (null if none exists).If multiple records m<strong>at</strong>ch, return an arbitrary one.@param k The key of the record to find */public E find(Key k);/** @return The number of records in the dictionary. */public int size();};Figure 4.29 The ADT for a simple dictionary.Figure 4.29 shows the definition for a simple abstract dictionary class. Themethods insert <strong>and</strong> find are the heart of the class. Method insert takes arecord <strong>and</strong> inserts it into the dictionary. Method find takes a key value <strong>and</strong> returnssome record from the dictionary whose key m<strong>at</strong>ches the one provided. If there aremultiple records in the dictionary with th<strong>at</strong> key value, there is no requirement as towhich one is returned.Method clear simply re-initializes the dictionary. The remove method issimilar to find, except th<strong>at</strong> it also deletes the record returned from the dictionary.Once again, if there are multiple records in the dictionary th<strong>at</strong> m<strong>at</strong>ch the desiredkey, there is no requirement as to which one actually is removed <strong>and</strong> returned.Method size returns the number of elements in the dictionary.The remaining Method is removeAny. This is similar to remove, exceptth<strong>at</strong> it does not take a key value. Instead, it removes an arbitrary record from thedictionary, if one exists. The purpose of this method is to allow a user the abilityto iter<strong>at</strong>e over all elements in the dictionary (of course, the dictionary will becomeempty in the process). Without the removeAny method, a dictionary user could

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

Saved successfully!

Ooh no, something went wrong!