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

Slides:



Advertisements
Similar presentations
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Advertisements

Stacks Chapter 11.
CHAPTER 7 Queues.
Fundamentals of Python: From First Programs Through Data Structures
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues and Priority Queues
CS Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of List, Stack and Queue.
COSC2006 Chapter 8 Queues III
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
TCSS 342, Winter 2005 Lecture Notes
Queue, Deque, and Priority Queue Implementations.
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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.
CSC 205 Programming II Lecture 22 Carwash Simulation.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
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 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Queues Chapter 8 (continued)
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.
Queues Queues Queues.
Priority Queue.
Chapter 13 Queues and Priority Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues, Deques and Priority Queues
Queues 11/9/2018 6:32 PM Queues.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Recitation 5 CS0445 Data Structures
Adapted from Pearson Education, Inc.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Queues CSC212.
Queues.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Queues, Deques, and Priority Queues Chapter 14

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

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.

4 Specifications for the ADT Queue Some everyday queues.

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. newEntry an object to be added */ public void enqueue(Object newEntry); /** Task: Removes and returns the front of the queue. 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. 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. true if the queue is empty */ public boolean isEmpty(); /** Task: Removes all entries from the queue. */ public void clear(); } // end queueInterface

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.

7 Using a Queue to Simulate a Waiting Line A line, or queue of people. Short waiting time increase satisfaction.

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.

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

10 Classes WaitLine and Customer A simulated waiting line … continued →

11 Classes WaitLine and Customer (ctd) A simulated waiting line.

12 Pseudocode of Method Simulate Algorithm 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++ }

13 Using a Queue to Compute Capital Gain in a Sale of Stock Buy n shares of a stock for d dollars each. Later you sell some of these shares. If the sale price exceeds the purchase price, you have made a profit - a capital gain. Otherwise, you experience a loss. Let’s assume you sell shares in the order in which you purchase them. ( first-in, first- out)

14 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

15 Classes StockLedger and StockPurchase A diagram of the class StockLedger and StockPurchase.

16 30 shares at $45 each two queues (a) Adds each of the 30 individual shares to a queue (b) Encapsulate 30 shares into one object If we only wan to sell 20 shares with 10 shares left. (a) is easy to implement. (b). Need to place 10 left shares to the front of the queue.

17 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

18 Specifications of the ADT Deque An instance of a deque.

19 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

20 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.

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: 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

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

23 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.