queues, deques, and priority queues chapter 23 slides by steve armstrong letourneau university...

31
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Upload: oliver-moore

Post on 22-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Queues, Deques, and

Priority Queues

Chapter 23

Slides by Steve ArmstrongLeTourneau University

Longview, TX2007,Prentice Hall

Page 2: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents• Specifications for the ADT Queue• Using a Queue to Simulate a Waiting Line

The Classes WaitLine and Customer

• Using a Queue to Compute the capital Gain in a Sale of Stock The Classes StockLedger and StockPurchase

• Java Class Library: the Interface Queue• Specifications of the ADT Deque

Page 3: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents

• Using a Deque to Compute the Capital Gain in a Sale of Stock

• Specifications of the ADT Priority Queue

• Using a Priority Queue to Compute the Capital Gain in a Sale of Stock

• Specifications of the ADT Priority Queue

• Using a Priority Queue to Track Your Assignments

Page 4: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue 1

• Queue organizes entries according to order of entry Exhibits first-in, first-out behavior Contrast to stack which is last-in, first-out

• All additions are at the back of the queue

• Front of queue has items added first

Page 5: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue

Fig. 23-1 Some everyday queues.

Page 6: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue 2

• View interface for a queue of objects

• Note methods enqueue (item) add dequeue remove getFront (returns item) isEmpty clear

Page 7: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue 4

Fig. 23-2 Queue of strings after (a) enqueue adds Jim;

(b) Jess; (c) Jill; (d) Jane;

Page 8: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue

Fig. 23-2 Queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves, removes Jim; (g) enqueue adds

Jerry; (h) dequeue retrieves, removes Jess.

Page 9: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using a Queue to Simulate a Waiting Line 5

Fig. 23-3 A line, or queue of people.

Page 10: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes WaitLine and Customer

Fig. 23-4 A CRC card for the class WaitLine.

Page 11: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes WaitLine and Customer 6

Fig. 23-5 A diagram of the classes WaitLine and Customer.

Page 12: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes WaitLine and Customer

Fig. 23-6 A simulated waiting line … continued →

Page 13: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes WaitLine and Customer

Fig. 23-6 (ctd) A simulated waiting line.

Page 14: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

The Class Waitline

• View source code

• Note methods Constructor simulate displayResults reset

Page 15: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using a Queue to Compute Capital Gain in a Sale of Stock 10

• Must sell stocks in same order they were purchased Must use the difference in selling price and

purchase price to calculate capital gain

• We use a queue to Record investment transactions

chronologically Compute capital gain of the stock

Page 16: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes StockLedger and StockPurchase 11

Fig. 23-7 A CRC card for the class StockLedger

Page 17: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes StockLedger and StockPurchase

Fig. 23-8 A diagram of the class StockLedger and StockPurchase.

Page 18: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes StockLedger and StockPurchase 12

• View source code for class StockLedger

• Note method Constructor buy sell

Page 19: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Classes StockLedger and StockPurchase

Fig. 23-9 A queue of (a) individual shares of stock; (b) grouped shares.

Page 20: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Java Class Library: The Interface Queue

• Similar to our QueueInterface Specifies more methods

• Methods provided

Page 21: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque 15

• A Double ended queue

• Has operations that Add, remove, or retrieve entries At both its front and back

• Combines and expands the operations of queue and stack

Page 22: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque

Fig. 23-10 An instance of a deque.

Page 23: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque

• A Java interface

Page 24: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque

Fig. 23-11 A comparison of the operations that add, remove, and retrieve the entries of a stack s, queue q,

and a deque d; (a) add;

Page 25: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque

Fig. 23-11 A comparison of the operations that add, remove, and retrieve the entries of a stack s, queue q,

and a deque d; (b) remove;

Page 26: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Deque

Fig. 23-11 A comparison of the operations that add, remove, and retrieve the entries of a stack s, queue q,

and a deque d; (c) retrieve.

Page 27: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using a Deque to Compute Capital Gain in Sale of Stock 17

• Revise the class StockPurchase Represents purchase of n shares at d dollars per

share Constructor is changed Accessor methods for new fields added

• Revise class StockLedger Method ledger now an instance of a deque Method buy now creates instance of StockPurchase, places it at back of deque

Method sell also altered

Click to view new methodsClick to view new methods

Page 28: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Priority Queue

• Organizes objects according to priorities Contrast to regular queue in order of arrival

• Priority queue example – a hospital ER• Priority can be specified by an integer

Must define whether high number is high priority or …

Low number (say 0) is high priority

• Other objects can specify priority Object must have a compareTo method

Page 29: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications of the ADT Priority Queue

• Note specification for ADT priority queue

• Methods specified add remove //highest priority peek //retrieves highest priority getSize clear

Page 30: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using Priority Queue to Track Your Assignments 19

• Organize class or work assignments by due dates Early due date, higher priority

• Figure 23-2 diagram of class Assignment

Page 31: Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using Priority Queue to Track Your Assignments

• Fig. 23-13 A diagram of the class AssignmentLog

• Note source code of class