The List ADT
This chapter names the List abstract data type: the operations DynamicArray and LinkedList already share. We use an interface to bring those two otherwise unrelated classes under one contract. Then we repeat the same pattern with a second ADT, SortedList, which we implement as a sentinel-based SortedLinkedList.
After reading this chapter, you should be able to:
- Explain why duplicated
selectionSortmethods forDynamicArrayandLinkedListare a type-design problem, and how type substitution fixes it - Define a
List<T>interface for the operationsDynamicArrayandLinkedListshare, implement it withimplementsand@Override, and sort either kind of list with one method - Explain how an interface hides the cost of its implementation, such as
getmakingselectionSortrun in on an array-backed list and on a linked list - Define an abstract data type, and distinguish the
ListADT from its implementations - Document an ADT contract with Javadoc, including the
IndexOutOfBoundsExceptionbehavior for an invalid index - Name implementations after the structures that back them (
ArrayList,LinkedList), and program to the interface rather than to a specific implementation - Explain why a sorted structure cannot implement
List, and define aSortedListADT withoutsetthat requires comparable, non-null elements - Build a sentinel-based
SortedLinkedListwithadd,get,indexOf,contains, andremove, and explain why binary search does not give logarithmic running time on a linked list
You can download the accompanying code for this chapter: starter and solution.
Sections
- Run Selection Sort on LinkedList
- The List Interface
- Implementing a List Interface
- Operation Costs Behind the Interface
- Abstract Data Types
- Documenting the Contract
- Naming the Implementations
- Program to the Interface
- A Second ADT: SortedList
- A Class to Hold a Sorted List
- Adding in Order
- Access and Search
- Removing a Value