Top Banner
Queues, Deques, and Priority Queues Chapter 13
19

Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

Queues, Deques, and Priority Queues

Chapter 13

Page 2: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

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

Specifications of the ADT Deque Using a Deque 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 3: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

3

Specifications for the ADT Queue

Queue organizes entries according to order of entry,

Front of queue has items added first, end of queue has items added last. It organizes items chronologically Exhibits first-in, first-out behavior Contrast to stack which is last-in, first-out

All additions are at the back of the queue, removals are at the front of the queue.

Page 4: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

4

Specifications for the ADT Queue

Some everyday queues.

Page 5: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

5

Specifications for the ADT Queue Interface for a queue of objects

public 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 Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

6

Specifications for the ADT Queue

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 Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

7

Using a Queue to Simulate a Waiting Line

A line, or queue of people.

Short waiting time increase satisfaction.

Page 8: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

8

Waiting Line

Time-driven simulation, a counter enumerates simulated units of time - minutes etc.

Customers arrive at random times and are assigned a random transaction time. Math.random(); Maximal_transaction_time * Math.random()

At the conclusion, summary statistics are created including total number of customers served and the average waiting time.

Page 9: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

9

Classes WaitLine and Customer

A diagram of the classes WaitLine and Customer to generate a new customer

Page 10: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

10

Classes WaitLine and Customer

A simulated waiting line … continued →

Page 11: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

11

Classes WaitLine and Customer

(ctd) A simulated waiting line.

Page 12: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

12

Pseudocode of Method SimulateAlgorithm simulate (duration, arrivalProbability, maxTransactionTime)for( clock = 0; clock < duration; clock++){

if( a new customer arrives){ numberOfArrivals++ transactionTime = a random time does not exceed maxTransactionTime nextArrival = a new customer containing clock, transactionTime, and a customer number that is numberOfArrival

line.enqueue(nextArrival)}if( transactionTimeLeft > 0 ) // if present customer is still being served transactionTimeLeft—else if(!line.isEmpty)

{ nextCustomer = line.dequeue() transactionTimeLeft = nextCustomer.getTransactionTime() -1 timeWaited = clock() – nextCustomer.getArrivalTime() totalTimeWaited = totalTimeWaited + timeWaited numberServed++}

}

Page 13: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

13

Specifications of the ADT Deque

A Double ended queue At a post office, when it is your turn, you

need to fill up a form, step aside and get served when finished.

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

Combines and expands the operations of queue and stack

Page 14: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

14

Specifications of the ADT Deque

An instance of a deque.

Page 15: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

15

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 16: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

16

Specifications of the ADT Deque

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 17: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

17

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: assign a priority number to each patient to override the arrival time.

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 18: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

18

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 19: Queues, Deques, and Priority Queues Chapter 13. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.

19

Using Priority Queue to Track Your Assignment

Class TrackAssignment Include data field DueDate Method compareTo is made available This field is the priority – earliest due date is

highest priority Organize tasks in the order in which we should

complete them.