1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C 5.1-5.8, 9.3.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
Topic 9 The Queue ADT.
CHAPTER 7 Queues.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
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
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Queues What is a Queue? Queue Implementations: Queue As Array
Stacks, Queues, and Deques
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Data Structures - Queues
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
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.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Java Collections Framework The client view. The Big Picture.
The Queue ADT.
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.
Marcus Biel, Software Craftsman
Queues Implementations 6/17/2018.
Csc 2720 Data structure Instructor: Zhuojun Duan
COS 312 DAY 21 Tony Gauvin.
Queues Queue Concept Queue Design Considerations
Chapter 5 Queues.
Data Structures and Database Applications Queues in C#
Queues, Deques and Priority Queues
THURSDAY, OCTOBER 17 IN LAB
Queues, Deques and Priority Queues
Stacks, Queues, and Deques
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Java Collections Framework
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 4 Queues.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Queues.
Data Structures & Programming
Presentation transcript:

1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3

2 Queue Abstract Data Type A queue is a linear collection where the elements are added to one end and removed from the other end The processing is first in, first out (FIFO) The first element put on the queue is the first element removed from the queue Think of a line of people waiting for a bus (The British call that “queuing up”)

3 A Conceptual View of a Queue Rear of Queue (or Tail) Adding an Element Removing an Element Front of Queue (or Head)

4 Queue Terminology We enqueue an element on a queue to add one We dequeue an element off a queue to remove one We can also examine the first element without removing it We can determine if a queue is empty or not and how many elements it contains (its size) The L&C QueueADT interface supports the above operations and some typical class operations such as toString()

5 Queue Design Considerations Although a queue can be empty, there is no concept for it being full. An implementation must be designed to manage storage space For first and dequeue operation on an empty queue, this implementation will throw an exception Other implementations could return a value null that is equivalent to “nothing to return”

6 The java.util.Queue Interface The java.util.Queue interface is in the Java Collections API (extends Collection) However, it is only an interface and you must use an implementing class LinkedList is the most commonly used implementing class For a queue of type objects: Queue myQueue = new LinkedList ();

7 The java.util.Queue Interface The names of the methods are different Enqueue is done using: boolean offer(T element) // returns false if full Dequeue is done using either: T poll() // returns null value if empty T remove() // throws an exception if empty Peek is done using either: T peek() // returns null value if empty T element() // throws an exception if empty

The java.util.Deque Interface The Deque (pronounced like “deck”) interface defines an ADT for a double ended queue A Deque can be used as a queue or a stack (You can add or remove from either end) This interface extends the Queue interface This interface is implemented by the same LinkedList class that implements the Queue interface 8

9 Applications for a Queue A queue can be used as an underlying mechanism for many common applications –Cycling through a set of elements in order –Simulation of client-server operations –Radix Sort –Scheduling processes in an operating system such as printer queues

10 Cycling through Code Keys The Caesar cipher is simple letter shifting Each letter is treated as its number 0-25 in the alphabet and each letter is encoded as: cipher value = (letter value + constant) % 26 The message is decoded letter by letter: letter value = (cipher value – constant) % 26 if (letter value < 0) letter value += 26 Using the constant 7, the word “queue” would be coded as “xblbl” Note: the word’s “pattern” is recognizable

11 Cycling through Code Keys The Caesar cipher is easy to solve because there are only 26 possible “keys” to try It can be made harder by cycling through a key set of values such as 3, 1, 7, 4, 2, 5 We put that sequence of numbers in a queue As we encode each letter, we dequeue a number for the constant and re-enqueue it - cycling through the entire key set as many times as needed for the message length

12 Cycling through Code Keys Using that queue of numbers as the constant values, the word “queue” becomes “tvlyg” Note: the word’s “pattern” is not recognizable If we are encoding a message containing the entire Unicode character set, we can omit the modulo 26 operator as in the text book code See L&C, Listing 5.1

13 Ticket Counter Simulation See L&C Listing 5.2 and 5.3 The simulation in this example sets up the queue with customers arriving at regular 15 second intervals This is not the most meaningful analysis because it doesn’t take into account the typical variations in arrival rate One customer every 15 seconds could mean 8 customers arriving at one time and then 2 minutes with no arriving customers

14 Ticket Counter Simulation A more sophisticated simulation would use probability distributions for the arrival rate and for the processing time –With an average serving time that sets a maximum capacity for handling customers based on the number of servers –And an average arrival time with parameters for the distribution of arrivals over time A statistical analysis is more important for an ice cream shop next to a movie theater (during a movie versus as a movie lets out)

Ticket Counter Simulation Textbook code always gives same values: cashiers time

Ticket Counter Simulation Poisson Distribution is commonly used for estimating arrival times in simulations –Lambda is the average number of arrivals per time interval –P(X=k) is the probability that k is the number of arrivals during this rv this time interval 16

Ticket Counter Simulation Replacement for textbook code in listing 5.3: /** load customer queue improved to use random Poisson arrival times*/ Poisson myDist = new Poisson(1); for (int count=0; count < NUM_INTERVALS; count++) { int numberOfCustomers = myDist.getValue(); for (int i = 0; i < numberOfCustomers; i++) customerQueue.offer(new Customer(count*15)); } Introduces random arrival times based on the Poisson distribution for each time interval 17

18 Radix Sort See L&C Listing 9.3 Like the old IBM punched card sorters * * * Original List 1. Sort into Queues by Radix 2. Empty Each Queue in Order and Add to List Q0Q1 Q10

IBM Card Sorter 19