Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-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.
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.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
queues1 Queues Data structures that wait their turn.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
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,
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
© 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.
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.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
© 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.
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.
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)
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 (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
© 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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
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,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
The Queue ADT.
Stack: a Linked Implementation
Review Array Array Elements Accessing array elements
The List ADT.
Using Queues: Coded Messages
Queues Implementations 6/17/2018.
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.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 7 Queues

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-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 © 2004 Pearson Addison-Wesley. All rights reserved.1-3 Queues A queue is a collection whose elements are added on one end and removed from another 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-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 © 2004 Pearson Addison-Wesley. All rights reserved.1-5 figure 7.1 A conceptual view of a queue

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-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 © 2004 Pearson Addison-Wesley. All rights reserved.1-7 figure 7.2 The operations on a queue See QueueADT.java (page 180)QueueADT.java

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-8 figure 7.3 The QueueADT interface in UML

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-9 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-10 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-11 figure 7.4 An encoded message using a repeating key

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-12 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 See Codes.java (page 183)Codes.java

figure 7.5 UML description of the Codes program

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-14 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 See Customer.java (page 186)Customer.java See TicketCounter.java (page 188)TicketCounter.java

figure 7.6 UML description of the TicketCounter program

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-16 figure 7.7 The results of the ticket counter simulation

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-17 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 in the sort key For example, if the sort key was 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-18 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-19 figure 7.8 A radix sort of ten three-digit numbers

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-20 Radix Sort Now let's look at a program that implements a radix sort It maintains an array of 10 queues, one for each digit (0-9) See RadixSort.java (page 194)RadixSort.java

figure 7.9 UML description of the RadixSort program

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-22 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-23 figure 7.10 A linked implementation of a queue

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-24 The enqueue Operation // // Adds the specified element to the rear of the queue. // public void enqueue (Object element) { LinearNode temp = new LinearNode (element); if (isEmpty()) front = node; else rear.setNext (node); rear = node; count++; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-25 figure 7.11 The queue after adding element E

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-26 The dequeue Operation // // Removes the element at the front of the queue and returns a // reference to it. Throws an EmptyCollectionException if the // queue is empty. // public Object dequeue () throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); Object result = front.getElement(); front = front.getNext(); count--; if (isEmpty()) rear = null; return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-27 figure 7.12 The queue after a dequeue operation

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-28 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-29 figure 7.13 An array implementation of a queue

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-30 The enqueue Operation // // Adds the specified element to the rear of the queue, expanding // the capacity of the queue array if necessary. // public void enqueue (Object element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear++; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-31 figure 7.14 The queue after adding element E

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-32 The dequeue Operation // // Removes the element at the front of the queue and returns a // reference to it. Throws an EmptyCollectionException if the // queue is empty. // public Object dequeue () throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); Object result = queue[0]; // shift the elements for (int scan=0; scan < rear; scan++) queue[scan] = queue[scan+1]; rear--; queue[rear] = null; return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-33 figure 7.15 The queue after removing the first element

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-34 The CircularArrayQueue Class If we don't fix one end of the queue at index 0, we won't 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

figure 7.16 A circular array implementation of a queue

figure 7.17 A queue straddling the end of a circular array

figure 7.18 Changes in a circular array implementation of a queue

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-38 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 © 2004 Pearson Addison-Wesley. All rights reserved.1-39 Queue Implementations The linked implementation of a queue does not suffer from the need to operate on both ends of the queue 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