Queues, Deques and Priority Queues Chapter 10 Adapted from Pearson Education, Inc.
Contents The ADT Queue The ADT Deque The ADT Priority Queue A Problem Solved: Simulating a Waiting Line A Problem Solved: Computing the Capital Gain in a Sale of Stock Java Class Library: The Interface Queue The ADT Deque Java Class Library: The Interface Deque Java Class Library: The Class ArrayDeque The ADT Priority Queue A Problem Solved: Tracking Your Assignments Java Class Library: The Class PriorityQueue Adapted from Pearson Education, Inc.
Objectives Describe operations of ADT queue Use queue to simulate waiting line Use queue in programs that organizes data in first-in, first-out manner Describe operations of ADT deque Use deque in program that organizes data chronologically and can operate on both oldest and newest entries Describe operations of ADT priority queue Use priority queue in program that organizes data objects according to priorities Adapted from Pearson Education, Inc.
ADT Queue Another name for a waiting line Used within operating systems Simulate real world events First in, first out (FIFO) Adapted from Pearson Education, Inc.
ADT Queue Another name for a waiting line Used within operating systems Simulate real world events First in, first out (FIFO) Adapted from Pearson Education, Inc.
ADT Queue A collection of objects in chronological order and having the same data type Operations enqueue(newEntry) dequeue() getFront() isEmpty() clear() B FALSE X G A X Adapted from Pearson Education, Inc.
ADT Queue A collection of objects in chronological order and having the same data type Operations enqueue(newEntry) dequeue() getFront() isEmpty() clear() A X True Adapted from Pearson Education, Inc.
Queue Interface Adapted from Pearson Education, Inc. public interface QueueInterface < T > { /** Adds a new entry to the back of the queue. @param newEntry an object to be added */ public void enqueue (T newEntry); /** Removes and returns the entry at the front of this queue. @return either the object at the front of the queue or, if the queue is empty before the operation, null */ public T dequeue (); /** Retrieves the entry at the front of this queue. @return either the object at the front of the queue or, if the queue is empty, null */ public T getFront (); /** Detects whether this queue is empty. @return true if the queue is empty, or false otherwise */ public boolean isEmpty (); /** Removes all entries from this queue. */ public void clear (); } // end QueueInterface Adapted from Pearson Education, Inc.
Simulating a Waiting Line Adapted from Pearson Education, Inc.
A diagram of the classes Adapted from Pearson Education, Inc.
Algorithm for simulate Adapted from Pearson Education, Inc.
Figure 10-6 A simulated waiting line Adapted from Pearson Education, Inc.
Figure 10-6 A simulated waiting line Adapted from Pearson Education, Inc.
Class WaitLine Implementation of class WaitLine Listing 10-2 Statements Generate line for 20 minutes 50 percent arrival probability 5-minute maximum transaction time. View sample output Adapted from Pearson Education, Inc.
Java Class Library: Queue public boolean add(T newEntry) public boolean offer(T newEntry) public T remove() public T poll() public T element() public T peek() public boolean isEmpty() public void clear() public int size() Adapted from Pearson Education, Inc.
ADT Deque Need for an ADT which offers Double ended queue Add, remove, retrieve At both front and back of a queue Double ended queue Called a deque Pronounced “deck” Actually behaves more like a double ended stack Adapted from Pearson Education, Inc.
Deque Interface public interface DequeInterface < T > { public void addToFront (T newEntry); public void addToBack (T newEntry); public T removeFront (); public T removeBack (); public T getFront (); public T getBack (); public boolean isEmpty (); public void clear (); } // end DequeInterface Adapted from Pearson Education, Inc.
Java Class Library: Deque public void addFirst(T newEntry) public boolean offerFirst(T newEntry) public void addLast(T newEntry) public boolean offerLast(T newEntry) public T removeFirst() public T pollFirst() public T removeLast() public T pollLast() public T getFirst() public T peekFirst() public T getLast() Public T peekLast() public boolean isEmpty() public void clear() public int size() Adapted from Pearson Education, Inc.
ADT Priority Queue Contrast bank queue and emergency room queue(s) ADT priority queue organizes objects according to their priorities You have to be able to sort the items that you use in a PQ. Adapted from Pearson Education, Inc.
Priority Queue Interface public interface PriorityQueueInterface < T extends Comparable < ? super T >> { /** Adds a new entry to this priority queue. @param newEntry an object */ public void add (T newEntry); /** Removes and returns the item with the highest priority. @return either the object with the highest priority or, if the priority queue is empty before the operation, null */ public T remove (); /** Retrieves the item with the highest priority. @return either the object with the highest priority or, if the priority queue is empty, null */ public T peek (); Adapted from Pearson Education, Inc.
Priority Queue Interface /** Detects whether this priority queue is empty. @return true if the priority queue is empty, or false otherwise */ public boolean isEmpty (); /** Gets the size of this priority queue. @return the number of entries currently in the priority queue */ public int getSize (); /** Removes all entries from this priority queue */ public void clear (); } // end PriorityQueueInterface Adapted from Pearson Education, Inc.
Problem: Tracking Your Assignments Consider tasks assigned with due dates We use a priority queue to organize in due date order Adapted from Pearson Education, Inc.
Java Class Library: PriorityQueue public PriorityQueue() public PriorityQueue(int initialCapacity) public boolean add(T newEntry) public boolean offer(T newEntry) public T remove() public T poll() public T element() public T peek() public boolean isEmpty() public void clear() public int size() Adapted from Pearson Education, Inc.
End Chapter 10 Adapted from Pearson Education, Inc.