Download presentation
Presentation is loading. Please wait.
1
Queues, Deques, and Priority Queues
Chapter 10 Data Structures and Abstractions with Java, 4e, Global Edition Frank Carrano © 2016 Pearson Education, Ltd. All rights reserved.
2
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue A queue is another name for a waiting line Used within operating systems and to simulate real-world events Come into play whenever processes or events must wait Entries organized first-in, first-out © 2016 Pearson Education, Ltd. All rights reserved.
3
The ADT Queue FIGURE 10-1 Some everyday queues
© 2016 Pearson Education, Ltd. All rights reserved.
4
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue Terminology Item added first, or earliest, is at the front of the queue Item added most recently is at the back of the queue Additions to a software queue must occur at its back Client can look at or remove only the entry at the front of the queue © 2016 Pearson Education, Ltd. All rights reserved.
5
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.
6
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.
7
The ADT Queue LISTING 10-1 An interface for the ADT queue
© 2016 Pearson Education, Ltd. All rights reserved.
8
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue FIGURE 10-2 A queue of strings after (a) enqueue adds Jim; (b) enqueue adds Jess; (c) enqueue adds Jill; (d) enqueue adds Jane; © 2016 Pearson Education, Ltd. All rights reserved.
9
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Queue FIGURE 10-2 A queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves and removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves and removes Jess © 2016 Pearson Education, Ltd. All rights reserved.
10
Simulating a Waiting Line
FIGURE 10-3 A line, or queue, of people © 2016 Pearson Education, Ltd. All rights reserved.
11
Simulating a Waiting Line
FIGURE 10-4 A CRC card for the class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
12
Simulating a Waiting Line
FIGURE 10-5 A diagram of the classes WaitLine and Customer © 2016 Pearson Education, Ltd. All rights reserved.
13
Simulating a Waiting Line
FIGURE 10-5 A diagram of the classes WaitLine and Customer © 2016 Pearson Education, Ltd. All rights reserved.
14
Simulating a Waiting Line
Algorithm for simulate © 2016 Pearson Education, Ltd. All rights reserved.
15
Simulating a Waiting Line
FIGURE 10-6 A simulated waiting line © 2016 Pearson Education, Ltd. All rights reserved.
16
Simulating a Waiting Line
FIGURE 10-6 A simulated waiting line © 2016 Pearson Education, Ltd. All rights reserved.
17
Simulating a Waiting Line
LISTING 10-2 The class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
18
Simulating a Waiting Line
LISTING 10-2 The class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
19
Simulating a Waiting Line
LISTING 10-2 The class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
20
Simulating a Waiting Line
LISTING 10-2 The class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
21
Simulating a Waiting Line
LISTING 10-2 The class WaitLine © 2016 Pearson Education, Ltd. All rights reserved.
22
Computing the Capital Gain in a Sale of Stock
FIGURE 10-7 A CRC card for the class StockLedger © 2016 Pearson Education, Ltd. All rights reserved.
23
Computing the Capital Gain in a Sale of Stock
FIGURE 10-8 A diagram of the classes StockLedger and StockPurchase © 2016 Pearson Education, Ltd. All rights reserved.
24
Computing the Capital Gain in a Sale of Stock
LISTING 10-3 The class StockLedger © 2016 Pearson Education, Ltd. All rights reserved.
25
Computing the Capital Gain in a Sale of Stock
LISTING 10-3 The class StockLedger © 2016 Pearson Education, Ltd. All rights reserved.
26
Computing the Capital Gain in a Sale of Stock
LISTING 10-3 The class StockLedger © 2016 Pearson Education, Ltd. All rights reserved.
27
Computing the Capital Gain in a Sale of Stock
FIGURE 10-9 A queue of (a) individual shares of stock; (b) grouped shares © 2016 Pearson Education, Ltd. All rights reserved.
28
Java Class Library: The Interface Queue
Methods provided add offer remove poll element peek isEmpty size © 2016 Pearson Education, Ltd. All rights reserved.
29
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Deque A double ended queue Deque pronounced “deck” Has both queuelike operations and stacklike operations © 2016 Pearson Education, Ltd. All rights reserved.
30
The ADT Deque FIGURE 10-10 An instance d of a deque
© 2016 Pearson Education, Ltd. All rights reserved.
31
The ADT Deque LISTING 10-4 An interface for the ADT deque
© 2016 Pearson Education, Ltd. All rights reserved.
32
The ADT Deque LISTING 10-4 An interface for the ADT deque
© 2016 Pearson Education, Ltd. All rights reserved.
33
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Deque FIGURE A comparison of operations for a stack s, a queue q, and a deque d: (a) add; (b) remove; (c) retrieve © 2016 Pearson Education, Ltd. All rights reserved.
34
© 2016 Pearson Education, Ltd. All rights reserved.
The ADT Deque Pseudocode that uses a deque to read and display a line of keyboard input © 2016 Pearson Education, Ltd. All rights reserved.
35
Computing the Capital Gain in a Sale of Stock
Method buy creates an instance of StockPurchase and places it at the back of the deque © 2016 Pearson Education, Ltd. All rights reserved.
36
Computing the Capital Gain in a Sale of Stock
The method sell is more involved © 2016 Pearson Education, Ltd. All rights reserved.
37
Computing the Capital Gain in a Sale of Stock
The method sell is more involved © 2016 Pearson Education, Ltd. All rights reserved.
38
Java Class Library: The Interface Deque
Methods provided addFirst, offerFirst addLast, offerLast removeFirst, pollFirst removeLast, pollLast getFirst, peekFirst getLast, peekLast isEmpty, clear, size push, pop © 2016 Pearson Education, Ltd. All rights reserved.
39
Java Class Library: The Class ArrayDeque
Implements the interface Deque Constructors provided ArrayDeque() ArrayDeque(int initialCapacity) © 2016 Pearson Education, Ltd. All rights reserved.
40
© 2016 Pearson Education, Ltd. All rights reserved.
ADT Priority Queue Consider how a hospital assigns a priority to each patient that overrides time at which patient arrived. ADT priority queue organizes objects according to their priorities Definition of “priority” depends on nature of the items in the queue © 2016 Pearson Education, Ltd. All rights reserved.
41
ADT Priority Queue LISTING 10-5 An interface for the ADT priority queue © 2016 Pearson Education, Ltd. All rights reserved.
42
ADT Priority Queue LISTING 10-5 An interface for the ADT priority queue © 2016 Pearson Education, Ltd. All rights reserved.
43
Tracking Your Assignments
FIGURE A diagram of the class Assignment © 2016 Pearson Education, Ltd. All rights reserved.
44
Tracking Your Assignments
FIGURE A diagram of the class AssignmentLog © 2016 Pearson Education, Ltd. All rights reserved.
45
Tracking Your Assignments
LISTING 10-6 The class AssignmentLog © 2016 Pearson Education, Ltd. All rights reserved.
46
Tracking Your Assignments
LISTING 10-6 The class AssignmentLog © 2016 Pearson Education, Ltd. All rights reserved.
47
Java Class Library: The Class PriorityQueue
Basic constructors and methods PriorityQueue add offer remove poll element peek isEmpty, clear, size © 2016 Pearson Education, Ltd. All rights reserved.
48
© 2016 Pearson Education, Ltd. All rights reserved.
End Chapter 10 © 2016 Pearson Education, Ltd. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.