Queues, Deques, and Priority Queues

Slides:



Advertisements
Similar presentations
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Advertisements

Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Stacks, Queues & Deques CSC212.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Queues and Priority Queues
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues Chapter 8 (continued)
Data Abstraction & Problem Solving with C++
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
Double-Ended Queues Chapter 5.
Lecture 7 Queues Stacks Trees.
Marcus Biel, Software Craftsman
Csc 2720 Data structure Instructor: Zhuojun Duan
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Priority Queue.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 13 Queues and Priority Queues
Building Java Programs
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Queue.
Stack Implementations
Queues CSC212.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Copyright © Aiman Hanna All rights reserved
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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.

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

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

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

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

© 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 Pearson Education, Ltd.  All rights reserved.

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

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

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 WaitLine © 2016 Pearson Education, Ltd.  All rights reserved.

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

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

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 Education, Ltd.  All rights reserved.

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 WaitLine © 2016 Pearson Education, Ltd.  All rights reserved.

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

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

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

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

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.

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.

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

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

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

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 element peek isEmpty size © 2016 Pearson Education, Ltd.  All rights reserved.

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

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 Pearson Education, Ltd.  All rights reserved.

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

© 2016 Pearson Education, Ltd. All rights reserved. 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.

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

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.

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 involved © 2016 Pearson Education, Ltd.  All rights reserved.

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.

Java Class Library: The Class ArrayDeque Implements the interface Deque Constructors provided ArrayDeque() ArrayDeque(int initialCapacity) © 2016 Pearson Education, Ltd.  All rights reserved.

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

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 © 2016 Pearson Education, Ltd.  All rights reserved.

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 AssignmentLog © 2016 Pearson Education, Ltd.  All rights reserved.

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

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

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.

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