Queues Deques and Priority Queues Chapter 10 Data

  • Slides: 48
Download presentation
Queues, Deques, and Priority Queues Chapter 10 Data Structures and Abstractions with Java, 4

Queues, Deques, and Priority Queues Chapter 10 Data Structures and Abstractions with Java, 4 e, Global Edition Frank Carrano © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue • A queue is another name for a waiting line •

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.

The ADT Queue FIGURE 10 -1 Some everyday queues © 2016 Pearson Education, Ltd.

The ADT Queue FIGURE 10 -1 Some everyday queues © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue • Terminology § Item added first, or earliest, is at the

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.

The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue LISTING 10 -1 An interface for the ADT queue © 2016

The ADT Queue LISTING 10 -1 An interface for the ADT queue © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Queue FIGURE 10 -2 A queue of strings after (a) enqueue adds

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.

The ADT Queue FIGURE 10 -2 A queue of strings after (e) enqueue adds

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.

Simulating a Waiting Line FIGURE 10 -3 A line, or queue, of people ©

Simulating a Waiting Line FIGURE 10 -3 A line, or queue, of people © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line FIGURE 10 -4 A CRC card for the class Wait.

Simulating a Waiting Line FIGURE 10 -4 A CRC card for the class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line FIGURE 10 -5 A diagram of the classes Wait. Line

Simulating a Waiting Line FIGURE 10 -5 A diagram of the classes Wait. Line and Customer © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line FIGURE 10 -5 A diagram of the classes Wait. Line

Simulating a Waiting Line FIGURE 10 -5 A diagram of the classes Wait. Line and Customer © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line Algorithm for simulate © 2016 Pearson Education, Ltd. All rights

Simulating a Waiting Line Algorithm for simulate © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line FIGURE 10 -6 A simulated waiting line © 2016 Pearson

Simulating a Waiting Line FIGURE 10 -6 A simulated waiting line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line FIGURE 10 -6 A simulated waiting line © 2016 Pearson

Simulating a Waiting Line FIGURE 10 -6 A simulated waiting line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson

Simulating a Waiting Line LISTING 10 -2 The class Wait. Line © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock FIGURE 10 -7 A CRC

Computing the Capital Gain in a Sale of Stock FIGURE 10 -7 A CRC card for the class Stock. Ledger © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock FIGURE 10 -8 A diagram

Computing the Capital Gain in a Sale of Stock FIGURE 10 -8 A diagram of the classes Stock. Ledger and Stock. Purchase © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class Stock. Ledger © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class Stock. Ledger © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class

Computing the Capital Gain in a Sale of Stock LISTING 10 -3 The class Stock. Ledger © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock FIGURE 10 -9 A queue

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.

Java Class Library: The Interface Queue Methods provided • • add offer remove poll

Java Class Library: The Interface Queue Methods provided • • add offer remove poll element peek is. Empty size © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Deque • A double ended queue • Deque pronounced “deck” • Has

The ADT Deque • A double ended queue • Deque pronounced “deck” • Has both queuelike operations and stacklike operations © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Deque FIGURE 10 -10 An instance d of a deque © 2016

The ADT Deque FIGURE 10 -10 An instance d of a deque © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Deque LISTING 10 -4 An interface for the ADT deque © 2016

The ADT Deque LISTING 10 -4 An interface for the ADT deque © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Deque LISTING 10 -4 An interface for the ADT deque © 2016

The ADT Deque LISTING 10 -4 An interface for the ADT deque © 2016 Pearson Education, Ltd. All rights reserved.

The ADT Deque FIGURE 10 -11 A comparison of operations for a stack s,

The ADT Deque FIGURE 10 -11 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.

The ADT Deque Pseudocode that uses a deque to read and display a line

The ADT Deque Pseudocode that uses a deque to read and display a line of keyboard input © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock Method buy creates an instance

Computing the Capital Gain in a Sale of Stock Method buy creates an instance of Stock. Purchase and places it at the back of the deque © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock The method sell is more

Computing the Capital Gain in a Sale of Stock The method sell is more involved © 2016 Pearson Education, Ltd. All rights reserved.

Computing the Capital Gain in a Sale of Stock The method sell is more

Computing the Capital Gain in a Sale of Stock The method sell is more involved © 2016 Pearson Education, Ltd. All rights reserved.

Java Class Library: The Interface Deque Methods provided • add. First, offer. First •

Java Class Library: The Interface Deque Methods provided • add. First, offer. First • • add. Last, offer. Last remove. First, poll. First remove. Last, poll. Last get. First, peek. First get. Last, peek. Last is. Empty, clear, size push, pop © 2016 Pearson Education, Ltd. All rights reserved.

Java Class Library: The Class Array. Deque • Implements the interface Deque • Constructors

Java Class Library: The Class Array. Deque • Implements the interface Deque • Constructors provided § Array. Deque() § Array. Deque(int initial. Capacity) © 2016 Pearson Education, Ltd. All rights reserved.

ADT Priority Queue • Consider how a hospital assigns a priority to each patient

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.

ADT Priority Queue LISTING 10 -5 An interface for the ADT priority queue ©

ADT Priority Queue LISTING 10 -5 An interface for the ADT priority queue © 2016 Pearson Education, Ltd. All rights reserved.

ADT Priority Queue LISTING 10 -5 An interface for the ADT priority queue ©

ADT Priority Queue LISTING 10 -5 An interface for the ADT priority queue © 2016 Pearson Education, Ltd. All rights reserved.

Tracking Your Assignments FIGURE 10 -12 A diagram of the class Assignment © 2016

Tracking Your Assignments FIGURE 10 -12 A diagram of the class Assignment © 2016 Pearson Education, Ltd. All rights reserved.

Tracking Your Assignments FIGURE 10 -13 A diagram of the class Assignment. Log ©

Tracking Your Assignments FIGURE 10 -13 A diagram of the class Assignment. Log © 2016 Pearson Education, Ltd. All rights reserved.

Tracking Your Assignments LISTING 10 -6 The class Assignment. Log © 2016 Pearson Education,

Tracking Your Assignments LISTING 10 -6 The class Assignment. Log © 2016 Pearson Education, Ltd. All rights reserved.

Tracking Your Assignments LISTING 10 -6 The class Assignment. Log © 2016 Pearson Education,

Tracking Your Assignments LISTING 10 -6 The class Assignment. Log © 2016 Pearson Education, Ltd. All rights reserved.

Java Class Library: The Class Priority. Queue Basic constructors and methods • • Priority.

Java Class Library: The Class Priority. Queue Basic constructors and methods • • Priority. Queue add offer remove poll element peek is. Empty, clear, size © 2016 Pearson Education, Ltd. All rights reserved.

End Chapter 10 © 2016 Pearson Education, Ltd. All rights reserved.

End Chapter 10 © 2016 Pearson Education, Ltd. All rights reserved.