Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Data Structures and Algorithms (60-254)
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.
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
Fundamentals of Python: From First Programs Through Data Structures
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
TCSS 342, Winter 2005 Lecture Notes
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
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.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Chapter 7 Queues Introduction Queue applications Implementations.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
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,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
The Queue ADT.
Review Array Array Elements Accessing array elements
Queues Implementations 6/17/2018.
Csc 2720 Data structure Instructor: Zhuojun Duan
COS 312 DAY 21 Tony Gauvin.
Queues Queue Concept Queue Design Considerations
Queue.
Queues Queues Queues.
Chapter 5 Queues.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues.
Week 13 - Wednesday CS221.
Presentation transcript:

Chapter 7 Queues

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-3 Queues A queue is a collection whose elements are added on one end and removed from the other Therefore a queue is processed in a FIFO fashion: first in, first out Elements are removed in the same order they arrive Any waiting line is a queue: – the check out line at a grocery store – the cars at a stop light – an assembly line

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-4 Queues A queue is usually depicted horizontally One end of the queue is the rear (or tail), where elements are added The other end is the front (or head), from which elements are removed Unlike a stack, which operates on one end of the collection, a queue operates on both ends

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-5 FIGURE 7.1 A conceptual view of a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-6 Queue Operations The term enqueue is used to refer to the process of adding an element to a queue Likewise, dequeue is the process of removing an element Like a stack, a pure queue does not allow the user to access the elements in the middle of the queue We include a toString method for convenience

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-7 FIGURE 7.2 The operations on a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-8 FIGURE 7.3 The QueueADT interface in UML

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-9 Listing 7.1

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Coded Messages Let's use a queue to help us encode and decode messages A Ceasar cipher encodes a message by shifting each letter in a message by a constant amount k If k is 5, A becomes F, B becomes G, etc. However, this is fairly easy to break An improvement can be made by changing how much a letter is shifted depending on where the letter is in the message

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Coded Messages A repeating key is a series of integers that determine how much each character is shifted For example, consider the repeating key The first character in the message is shifted 3, the next 1, the next 7, and so on When the key is exhausted, we just start over at the beginning of the key

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.4 An encoded message using a repeating key

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Coded Messages We'll use a queue to store the values of the key We'll dequeue a value when needed After using a key value, we then enqueue it back onto the end of the queue That way the queue represents the constantly cycling values in the key

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.5 UML description of the Codes program

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Ticket Counter Simulation Now let's use a queue to simulate the waiting line at a movie theatre The goal is to determine how many cashiers are needed to keep the wait time below 7 minutes We'll assume: – customers arrive on average every 15 seconds – processing a request takes two minutes once a customer reaches a cashier

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.3

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.3 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.4

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.4 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.4 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.6 UML description of the TicketCounter program

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.7 The results of the ticket counter simulation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Radix Sort Let's look at one more use of queues A radix sort uses queues to order a set of values A queue is created for each possible value of a position (or digit) in the sort key For example, if the sort key is a lowercase alphabetic string, there would be 26 queues If the sort key was a decimal integer, there would be 10 queues corresponding to the digits 0 through 9

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Radix Sort Each pass through the sort examines a particular position in the sort value The element is put on the queue corresponding to that position's value Processing starts with the least significant position (1s) to the most significant position The following example uses integers with only the digits 0 through 5

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.8 A radix sort of ten three-digit numbers

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.5

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.5 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 7.5 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.9 UML description of the RadixSort program

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The LinkedQueue Class Like a stack, a queue can be implemented using an underlying array or a linked list A linked version can use the LinearNode class yet again In addition to keeping a reference to the beginning of the list, we will keep a second reference to the end An integer count will keep track of the number of elements in the queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.10 A linked implementation of a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedQueue - the enqueue Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.11 The queue after adding element E

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedQueue - the dequeue Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.12 The queue after a dequeue operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The ArrayQueue Class A queue can be managed using an array in which index 0 represents one end An integer value rear represents the next open slot in the array and the number of elements currently in the queue The challenge with this approach is that a queue operates on both ends, so the elements in the array must be shifted to keep one end at index 0

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.13 An array implementation of a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayQueue - the enqueue Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.14 The queue after adding element E

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayQueue - the dequeue Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.15 The queue after removing the first element

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The CircularArrayQueue Class If we do not fix one end of the queue at index 0, we will not have to shift the elements A circular queue is an implementation of a queue using an array that conceptually loops around on itself That is, the last index is thought to precede index 0 We keep track of integers that indicate where the front and rear of the queue are at any given time

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.16 A circular array implementation of a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.17 A queue straddling the end of a circular array

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 7.18 Changes in a circular array implementation of a queue

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Circular Queues When an element is enqueued, the value of rear is incremented But it must take into account the need to loop back to 0: rear = (rear+1) % queue.length; Note that this array implementation can also reach capacity and may need enlarging

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Queue Implementations The enqueue operation is O(1) for all implementations The dequeue operation is O(1) for linked and circular array implementations, but O(n) for the noncircular array version due to the need to shift the elements in the queue