Chapter 24 Dispensers and dictionaries
This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues
Dispensers n A dispenser is a container that restricts access to its elements. n Only one element in the container can be accessed or removed. n This element is called the “current element.”
Dispensers (cont.) n A dispenser has three essential features: adding, removing, accessing. void add (Object obj) Add the specified element to this dispenser. void remove () Remove the current element from this dispenser. Object get () The current element of this dispenser.
Dispensers (cont.) n You can combine access and removal into one operation. Object dispenseItem () obj = dispenser.dispenseItem(); is equivalent to obj = dispenser.get(); dispenser.remove(); n The former is preferred
Stacks n stack: a dispenser in which the current element is the container element most recently added to the container. n Also known as last-in first-out, or LIFO lists.
Stacks (cont.) The features get, add, and remove traditionally are named top, push, and pop.
Stack implementations n We use the same bridge pattern for implementing dispensers as we did for implementing lists. u We provide the container with an implementation component so that we can extend the class to meet the needs of an application, independent of its implementations.
Stack implementations (cont.) n In an array-based implementation: n This permits pushing and popping to be done in constant time.
Stack implementations (cont.) n In a linked implementation: n Again, pushing and popping can be done in constant time.
Translating Stack features n In order to build Stack features into BoundedList or LinkedList, we could use an adapter class to translate the specifications of an existing class to those required. n We “wrap” the existing class in the new class.
Translating Stack features (cont.) n We can also adapt an existing class by extending it.
Queues n queue:a dispenser in which the current element is the container element least recently added to the container. n Also known as first-in first-out, or FIFO lists. The fundamental dispenser features are front, append, and remove ; append and remove traditionally are named enqueue and serve.
Queue implementations n If a LinkedList maintains references to both ends of the list, elements can be added to either end in constant time. n Constant time deletes can be done only from the front of the list. Therefore, we make the front of LinkedList the front of the queue. n An adapter class can be defined easily in much the same way as was done for Stacks.
Queue implementations (cont.) n If we attempt to use the class BoundedList to implement queues, we encounter a problem: u Adding and removing elements requires shuffling the list, and takes linear time.
Queue implementations (cont.) n Viewing an array as a circular structure permits all queue methods to operate in constant time.
Queue implementations (cont.) n The queue occupies a set of contiguous array elements, and “circulates” through the array as items are added and removed.
Queue implementations (cont.) n The relationship between the two indexes is the same for both the full queue and the empty queue. (rear+1)%n == front
Priority queue n priority queue: a dispenser in which the current element is a largest container element with respect to some given ordering. n A largest item with respect to the ordering is referred to as the highest priority item. The dispenser features highest, add, and remove ; add and remove are traditionally named enqueue and serve.
PriorityQueue implementations n A way to implement a priority queue is with the class OrderedList. n If we base a PriorityQueue implementation on either an array-based or linked implementation of OrderedList, add will be linear highest and remove can be made constant time operations u With an array-based implementation, we can find an element’s position in log n time.
Dictionaries n dictionary: a container in which the elements are accessed by key. n Dictionary entries have a key and an associated value. i.e. a key-value pair. u Example: English dictionary: an English word is the key, and the definition is the value. n We can also consider the key to be an attribute of the entry, rather than a separate component. u Example: telephone directory has name (key), address and number.
Dictionaries (cont.) n A key must be provided to access or delete an item. n We assume keys are unique.
Dictionary implementations n We can build straightforward implementations of dictionaries with a List whose elements are key-value pairs. The method get and remove can simply search the List to locate the item with the given key. java.util defines an interface Map that serves as a superclass for dictionary variants.
We’ve covered n Dispensers. u stacks u queues u priority queues n Dictionaries. n Implementations using BoundedList and LinkedList. n Method performance.
Glossary