23
Queues, Deques, and Priority Queues Chapter 22

Queues, Deques, and Priority Queues

  • Upload
    kaori

  • View
    67

  • Download
    0

Embed Size (px)

DESCRIPTION

Queues, Deques, and Priority Queues. Chapter 22. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Queues, Deques, and Priority Queues

Queues, Deques, and Priority Queues

Chapter 22

Page 2: Queues, Deques, and Priority Queues

2

Chapter Contents

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

Specifications of the ADT DequeUsing a Deque to Compute the Capital Gain in a Sale of StockSpecifications of the ADT Priority QueueUsing a Priority Queue to Compute the Capital Gain in a Sale of Stock

Page 3: Queues, Deques, and Priority Queues

3

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

Page 4: Queues, Deques, and Priority Queues

4

Specifications for the ADT Queue

Fig. 22-1 Some everyday queues.

Page 5: Queues, Deques, and Priority Queues

5

Specifications for the ADT Queue

Interface for a queue of objectspublic interface queueInterface{ /** Task: Adds a new entry to the back of the queue.

* @param newEntry an object to be added */public void enqueue(Object newEntry);

/** Task: Removes and returns the front of the queue.* @return either the object at the front of the queue or null if the queue was empty */public Object dequeue();

/** Task: Retrieves the front of the queue.* @return either the object at the front of the queue or null if the queue is empty */public Object getFront();

/** Task: Determines whether the queue is empty.* @return true if the queue is empty */public boolean isEmpty();

/** Task: Removes all entries from the queue. */public void clear();

} // end queueInterface

Page 6: Queues, Deques, and Priority Queues

6

Specifications for the ADT Queue

Fig. 22-2 Queue of strings after (a) enqueue adds Jim; (b) Jess; (c) Jill; (d) Jane; (e) Joe; (f) dequeue retrieves,

removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves, removes Jess.

Page 7: Queues, Deques, and Priority Queues

7

Using a Queue to Simulate a Waiting Line

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

Page 8: Queues, Deques, and Priority Queues

8

Classes WaitLine and Customer

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

Page 9: Queues, Deques, and Priority Queues

9

Classes WaitLine and Customer

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

Page 10: Queues, Deques, and Priority Queues

10

Classes WaitLine and Customer

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

Page 11: Queues, Deques, and Priority Queues

11

Classes WaitLine and Customer

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

Page 12: Queues, Deques, and Priority Queues

12

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

Page 13: Queues, Deques, and Priority Queues

13

Classes StockLedger and StockPurchase

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

Page 14: Queues, Deques, and Priority Queues

14

Classes StockLedger and StockPurchase

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

Page 15: Queues, Deques, and Priority Queues

15

Classes StockLedger and StockPurchase

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

Page 16: Queues, Deques, and Priority Queues

16

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

Page 17: Queues, Deques, and Priority Queues

17

Specifications of the ADT Deque

Fig. 22-10 An instance of a deque.

Page 18: Queues, Deques, and Priority Queues

18

Specifications of the ADT Deque

A Java interface

public interface DequeInterface{ public void addToFront(Object newEntry);

public void addToBack(Object newEntry);public Object removeFront();public Object removeBack();public Object getFront();public Object getBack();public boolean isEmpty();public void clear();

} // end DequeInterface

Page 19: Queues, Deques, and Priority Queues

19

Specifications of the ADT Deque

Fig. 22-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; (b) remove; (c) retrieve.

Page 20: Queues, Deques, and Priority Queues

20

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

Page 21: Queues, Deques, and Priority Queues

21

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 22: Queues, Deques, and Priority Queues

22

Specifications of the ADT Priority Queue

public interface PriorityQueueInterface{ public void add(Comparable newEntry);

public Comparable remove();public Comparable get();public boolean isEmpty();public int getSize();public void clear();

} // end PriorityQueueInterface

Page 23: Queues, Deques, and Priority Queues

23

Using Priority Queue to Compute Capital Gain in Sale of Stock

Revise class StockPurchase• Include data field date • Method compareTo is made available• This field is the priority – earliest date is

highest priority