Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.

Similar presentations


Presentation on theme: "Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All."— Presentation transcript:

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

2 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

3 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

4 Specifications for the ADT Queue 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

5 Specifications for the ADT Queue Fig. 23-1 Some everyday queues.

6 Specifications for the ADT Queue View interface for a queue of objects View interface Note methods – enqueue – dequeue – getFront – isEmpty – clear

7 Specifications for the ADT Queue Fig. 23-2 Queue of strings after (a) enqueue adds Jim; (b) Jess; (c) Jill; (d) Jane;

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

9 Using a Queue to Simulate a Waiting Line Fig. 23-3 A line, or queue of people.

10 Classes WaitLine and Customer Fig. 23-4 A CRC card for the class WaitLine.

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

12 Classes WaitLine and Customer Fig. 23-6 A simulated waiting line … continued →

13 Classes WaitLine and Customer Fig. 23-6 (ctd) A simulated waiting line.

14 The Class Waitline View source codesource code Note methods – Constructor – simulate – displayResults – reset

15 Using a Queue to Compute Capital Gain in a Sale of Stock 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

16 Classes StockLedger and StockPurchase Fig. 23-7 A CRC card for the class StockLedger

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

18 Classes StockLedger and StockPurchase View source code for class StockLedgersource code Note method – Constructor – buy – sell

19 Classes StockLedger and StockPurchase Fig. 23-9 A queue of (a) individual shares of stock; (b) grouped shares.

20 Java Class Library: The Interface Queue Similar to our QueueInterface – Specifies more methods Methods provided

21 Specifications of the ADT Deque 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

22 Specifications of the ADT Deque Fig. 23-10 An instance of a deque.

23 Specifications of the ADT Deque A Java interface

24 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;

25 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;

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

27 Using a Deque to Compute Capital Gain in Sale of Stock 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 methods

28 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

29 Specifications of the ADT Priority Queue Note specification for ADT priority queuespecification Methods specified – add – remove – peek – getSize – clear

30 Using Priority Queue to Track Your Assignments Organize class or work assignments by due dates – Early due date, higher priority Figure 23-2 diagram of class Assignment

31 Using Priority Queue to Track Your Assignments Fig. 23-13 A diagram of the class AssignmentLog Note source code of classsource code


Download ppt "Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All."

Similar presentations


Ads by Google