Queues, Deques and Priority Queues

Slides:



Advertisements
Similar presentations
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Advertisements

CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Stacks, Queues & Deques CSC212.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Queue, Deque, and Priority Queue Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Queues and Priority Queues
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Tables and Priority Queues
Queues Chapter 8 (continued)
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Csc 2720 Data structure Instructor: Zhuojun Duan
Queues Chapter 4.
Priority Queue.
Chapter 13 Queues and Priority Queues
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Queue.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 5 CS0445 Data Structures
Adapted from Pearson Education, Inc.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Queues CSC212.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Copyright © Aiman Hanna All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues.
CMPT 225 Lecture 8 – Queue.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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.