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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

138 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesclass. Unfortun<strong>at</strong>ely, this oblig<strong>at</strong>ion is hidden within the code of the dictionary (<strong>and</strong>possibly in the user’s manual) r<strong>at</strong>her than exposed in the dictionary’s interface. Asa result, some users of the dictionary might neglect to implement the overloading,with unexpected results. Again, the compiler will not c<strong>at</strong>ch this problem.The Java Comparable interface provides an approach to solving this problem.In a key-value pair implement<strong>at</strong>ion, the keys can be required to implementthe Comparable interface. In other applic<strong>at</strong>ions, the records might be requiredto implement ComparableThe most general solution is to have users supply their own definition for comparingkeys. The concept of a class th<strong>at</strong> does comparison (called a compar<strong>at</strong>or)is quite important. By making these oper<strong>at</strong>ions be generic parameters, the requirementto supply the compar<strong>at</strong>or class becomes part of the interface. This designis an example of the Str<strong>at</strong>egy design p<strong>at</strong>tern, because the “str<strong>at</strong>egies” for comparing<strong>and</strong> getting keys from records are provided by the client. Altern<strong>at</strong>ively, theComparable class allows the user to define the compar<strong>at</strong>or by implementing thecompareTo method. In some cases, it makes sense for the compar<strong>at</strong>or class toextract the key from the record type, as an altern<strong>at</strong>ive to storing key-value pairs.We will use the Comparable interface in Section 5.5 to implement comparisonin heaps, <strong>and</strong> in Chapter 7 to implement comparison in sorting algorithms.4.5 Further ReadingFor more discussion on choice of functions used to define the List ADT, see thework of the Reusable Software Research Group from Ohio St<strong>at</strong>e. Their definitionfor the List ADT can be found in [SWH93]. More inform<strong>at</strong>ion about designingsuch classes can be found in [SW94].4.6 Exercises4.1 Assume a list has the following configur<strong>at</strong>ion:〈 | 2, 23, 15, 5, 9 〉.Write a series of Java st<strong>at</strong>ements using the List ADT of Figure 4.1 to deletethe element with value 15.4.2 Show the list configur<strong>at</strong>ion resulting from each series of list oper<strong>at</strong>ions usingthe List ADT of Figure 4.1. Assume th<strong>at</strong> lists L1 <strong>and</strong> L2 are empty <strong>at</strong> thebeginning of each series. Show where the current position is in the list.(a) L1.append(10);L1.append(20);L1.append(15);

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

Saved successfully!

Ooh no, something went wrong!