Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues.

Similar presentations


Presentation on theme: "Chapter 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues."— Presentation transcript:

1 Chapter 24 Dispensers and dictionaries

2 This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues

3 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.”

4 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.

5 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

6 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.

7 Stacks (cont.) The features get, add, and remove traditionally are named top, push, and pop.

8

9

10 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.

11 Stack implementations (cont.) n In an array-based implementation: n This permits pushing and popping to be done in constant time.

12 Stack implementations (cont.) n In a linked implementation: n Again, pushing and popping can be done in constant time.

13 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.

14 Translating Stack features (cont.) n We can also adapt an existing class by extending it.

15 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.

16

17

18 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.

19 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.

20 Queue implementations (cont.) n Viewing an array as a circular structure permits all queue methods to operate in constant time.

21 Queue implementations (cont.) n The queue occupies a set of contiguous array elements, and “circulates” through the array as items are added and removed.

22 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

23

24

25

26 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.

27

28 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.

29 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.

30 Dictionaries (cont.) n A key must be provided to access or delete an item. n We assume keys are unique.

31

32

33 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.

34 We’ve covered n Dispensers. u stacks u queues u priority queues n Dictionaries. n Implementations using BoundedList and LinkedList. n Method performance.

35 Glossary


Download ppt "Chapter 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues."

Similar presentations


Ads by Google