Chapter 7 Queues Introduction Queue applications Implementations.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Queues.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
queues1 Queues Data structures that wait their turn.
Topic 9 The Queue ADT.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Fundamentals of Python: From First Programs Through Data Structures
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
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 reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies
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.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Queues and Priority Queues
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)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
CSC 205 Programming II Lecture 22 Carwash Simulation.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
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)
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
The Queue ADT.
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
CENG 213 Data Structure Queue 7/2/2018.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
CSC 143 Queues [Chapter 7].
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
ADT Queue (Array Implementation)
CE 221 Data Structures and Algorithms
Stacks, Queues, and Deques
Lecture 9: Stack and Queue
Queues.
Data Structures & Programming
Presentation transcript:

Chapter 7 Queues Introduction Queue applications Implementations

Ch. 7 Queues A queue is a First-In/First-Out linear data structure: a data item is inserted into the rear or taken out of the front of a sequence of elements, which means that they are removed (dequeued) in the same order as they are inserted (enqueued) A queue is a First-In/First-Out linear data structure: a data item is inserted into the rear or taken out of the front of a sequence of elements, which means that they are removed (dequeued) in the same order as they are inserted (enqueued) The queue ADT for one data type can essentially be the same for another data type, other than the type differences in method arguments and return values The queue ADT for one data type can essentially be the same for another data type, other than the type differences in method arguments and return values

ADT of Generic Queue Constructor Constructor public Queue ( ) /* initialize an empty queue */ Inquiry into the queue Inquiry into the queue public boolean isEmpty ( ) public boolean isEmpty ( ) /* Determine whether this queue is empty */ /* Determine whether this queue is empty */ public int size( ) public int size( ) /* return the current number of items in this queue */ /* return the current number of items in this queue */

ADT of ObjectQueue Adding an item at rear Adding an item at rear public void add (E item) /* add the given item as the last item of this queue */ Removing (and getting) an item from front Removing (and getting) an item from front public E remove ( ) /* remove and return the first item of this queue */

Think about using a queue The First-in/First-out and no-storage-restriction properties of a queue makes it a good candidate data structure for implementing the kind of processing where data come in sequence and need to be stored until certain conditions are met and then taken out to process, and where earlier data has priority The First-in/First-out and no-storage-restriction properties of a queue makes it a good candidate data structure for implementing the kind of processing where data come in sequence and need to be stored until certain conditions are met and then taken out to process, and where earlier data has priority

Example - checking palindromes A palindrome is a string that reads forward and backward the same. To check if a string is a palindrome, compare the string with the reversed string, character by character A palindrome is a string that reads forward and backward the same. To check if a string is a palindrome, compare the string with the reversed string, character by character ETTE

Example - checking palindromes T T E E

T T E E EETT

Example - car wash simulation The method carWashSimulate is the control center of a car wash The method carWashSimulate is the control center of a car wash An object of BooleanSource class is the random and probabilistic arrival of cars An object of BooleanSource class is the random and probabilistic arrival of cars An object of IntQueue class is the car waiting line An object of IntQueue class is the car waiting line An object of Averager class is the bookkeeper (tracking: number of cars, waiting time of a car, average waiting-time calculation; it is similar to, but simpler than, Statictician) An object of Averager class is the bookkeeper (tracking: number of cars, waiting time of a car, average waiting-time calculation; it is similar to, but simpler than, Statictician) An object of Washer class is the washing machine An object of Washer class is the washing machine

Example - car wash simulation In the method carWashSimulate: a loop cycles through every second of the simulation time, coordinating possible arrival of a car, sending the car to waiting queue, checking if the current car wash process has finished and if so taking another car from queue to wash, and updating car waiting statistics In the method carWashSimulate: a loop cycles through every second of the simulation time, coordinating possible arrival of a car, sending the car to waiting queue, checking if the current car wash process has finished and if so taking another car from queue to wash, and updating car waiting statistics In the BooleanSource object: each second of simulation, a car comes or does not, randomly, but with a probability that can be specified in advance In the BooleanSource object: each second of simulation, a car comes or does not, randomly, but with a probability that can be specified in advance

Example - car wash simulation In the Queue object: car arrive-time numbers enter at the rear and leave from the front In the Queue object: car arrive-time numbers enter at the rear and leave from the front In the Averager object: whenever a car is taken to wash this object is notified to update its record of car count, and also update with the waiting time of this car for the purpose of average waiting time calculation In the Averager object: whenever a car is taken to wash this object is notified to update its record of car count, and also update with the waiting time of this car for the purpose of average waiting time calculation In the Washer object: when it is not busy it can be started, and then it runs busy for a pre-set amount of time (with a manual clock device), and then becomes idle again In the Washer object: when it is not busy it can be started, and then it runs busy for a pre-set amount of time (with a manual clock device), and then becomes idle again

Example - car wash simulation Car Wash: Control Boolean Source: Car Arrival Queue: Car Waiting Averager: Car Waiting Statistics Washer: Car Washing

Array implementation of a queue Some difficulty with use of consecutive array positions when implementing a queue: the currently-in-use section gradually shifts. leaving empty space unused on one end but filling up the other end Some difficulty with use of consecutive array positions when implementing a queue: the currently-in-use section gradually shifts. leaving empty space unused on one end but filling up the other end Car 3Car 4Car 5 Car 6 Car 1 Car 2

Circular array A circular array is an array whose positions are used in a circular manner: when the rear end fills up but the front end has vacant positions the data will continue (wrap-around) to fill from the first position on, again; and two index variables are used to point to the front of the actual queue and the rear of the actual queue A circular array is an array whose positions are used in a circular manner: when the rear end fills up but the front end has vacant positions the data will continue (wrap-around) to fill from the first position on, again; and two index variables are used to point to the front of the actual queue and the rear of the actual queue Car 8Car 9Car 5Car 6Car 7 FrontRear

Queue ADT Invariant for array implementation The current number of items in the queue is stored in the instance variable manyItems The current number of items in the queue is stored in the instance variable manyItems For a non-empty queue, the items are stored in an instance variable circular array data, beginning at data[front] and continuing through data[rear]; both front and rear are instance variables For a non-empty queue, the items are stored in an instance variable circular array data, beginning at data[front] and continuing through data[rear]; both front and rear are instance variables For an empty queue, manyItems is 0, the data array does not contain legitimate items, and so there is no rule what front and rear must be For an empty queue, manyItems is 0, the data array does not contain legitimate items, and so there is no rule what front and rear must be

Method to calculate circular array position for next queue element This method takes an index integer and returns the next position index in the circular implementation of a queue This method takes an index integer and returns the next position index in the circular implementation of a queue private int nextIndex (int currentPosition) { if (++currentPosition == data.length ) return 0; else return currentPosition; } Already incremented

Using circular index - the getFront method of Generic ArrayQueue public E remove ( ) { if (manyItems == 0) throw new NoSuchElementException ( “Queue underflow.”); E answer = data[front]; front = nextIndex (front); manyItems --; return answer; }

Using circular index - the insert method of ObjectQueue public void add (E item) { if (manyItems == data.length) ensureCapacity (manyItems * 2 + 1); if (manyItems == 0) { front = 0; rear = 0; } else rear = nextIndex (rear); data[rear] = item; manyItems ++; }

The ensureCapacity method of ArrayQueue Car 4Car 5Car 1Car 2Car 3 rear front Car 1Car 2Car 3Car 4Car 5 rearfront

Linked list implementation of a queue Invariant of linked list implementation of the queue ADT Invariant of linked list implementation of the queue ADT – The number of items in the queue is stored in the instance variable manyItems – The items in the queue is stored in a linked list, with the first (head) node in instance variable front and last (tail) node in instance variable rear – When the queue is empty, there is no linked list and both front and rear contain the null reference Changes to the linked list occur only through adding at tail or removing at head Changes to the linked list occur only through adding at tail or removing at head

Priority queue A priority queue is a queue-like data structure where each item also has a priority tag, independent of when an item enters the queue relative to other items in queue A priority queue is a queue-like data structure where each item also has a priority tag, independent of when an item enters the queue relative to other items in queue – A item of highest priority will always be the item taken out from the queue – If there are several items of same highest priority, the one that enters the queue earliest will be taken out

Implementation of priority queue An implementation of priority queue An implementation of priority queue – In an instance variable is a data array of ArrayQueue type (or LinkedLinkedQueue type) – Each of the array position has a (ordinary) queue in itself containing items of a same priority – The array positions are for priorities 0, 1, 2, …, highest – A totalSize instance variable is used to keep track of all the queue sizes together