Download presentation
Presentation is loading. Please wait.
1
Queues, Deques and Priority Queues
Chapter 10 Adapted from Pearson Education, Inc.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
Queue Interface Adapted from Pearson Education, Inc.
public interface QueueInterface < T > { /** Adds a new entry to the back of the newEntry an object to be added */ public void enqueue (T newEntry); /** Removes and returns the entry at the front of this 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 either the object at the front of the queue or, if the queue is empty, null */ public T getFront (); /** Detects whether this queue is 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.
9
Figure 10-2 A queue of strings after (a) enqueue adds Jim; (b) enqueue adds Jess; (c) enqueue adds Jill; (d) enqueue adds Jane; Copyright ©2012 by Pearson Education, Inc. All rights reserved
10
Figure 10-2 A queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves and removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves and removes Jess; Copyright ©2012 by Pearson Education, Inc. All rights reserved
11
Simulating a Waiting Line
Adapted from Pearson Education, Inc.
12
A diagram of the classes
Adapted from Pearson Education, Inc.
13
Algorithm for simulate
Adapted from Pearson Education, Inc.
14
Figure 10-6 A simulated waiting line
Adapted from Pearson Education, Inc.
15
Figure 10-6 A simulated waiting line
Adapted from Pearson Education, Inc.
16
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.
17
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.
18
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.
19
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.
20
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.
21
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.
22
Priority Queue Interface
public interface PriorityQueueInterface < T extends Comparable < ? super T >> { /** Adds a new entry to this priority newEntry an object */ public void add (T newEntry); /** Removes and returns the item with the highest 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 either the object with the highest priority or, if the priority queue is empty, null */ public T peek (); Adapted from Pearson Education, Inc.
23
Priority Queue Interface
/** Detects whether this priority queue is true if the priority queue is empty, or false otherwise */ public boolean isEmpty (); /** Gets the size of this priority 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.
24
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.
25
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.
26
End Chapter 10 Adapted from Pearson Education, Inc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.