Copyright © Aiman Hanna All rights reserved

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues and Linked Lists
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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 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.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
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 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.
Double-Ended Queues Chapter 5.
Queues Rem Collier Room A1.02
Stacks.
Stacks and Queues.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Bucket-Sort and Radix-Sort
Array Lists, Node Lists & Sequences
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Copyright © Aiman Hanna All rights reserved
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Ordered Maps & Dictionaries
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,
Copyright © Aiman Hanna All rights reserved
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Copyright © Aiman Hanna All rights reserved
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Lecture 8: Stacks, Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Linked Lists
Presentation transcript:

Copyright © 2011-2016 Aiman Hanna All rights reserved Queues Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

The Queue ADT The Queue ADT stores arbitrary objects. Queues 4/6/2019 1:36 AM The Queue ADT The Queue ADT stores arbitrary objects. Insertions and deletions follow the first-in first-out (FIFO) scheme. Insertions are at the rear of the queue and removals are at the front of the queue. Main queue operations: enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front of the queue Queues

The Queue ADT Auxiliary queue operations: Exceptions Queues 4/6/2019 1:36 AM The Queue ADT Auxiliary queue operations: object front(): returns the element at the front without removing it. integer size(): returns the number of elements stored. boolean isEmpty(): indicates whether no elements are stored. Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues

Example Operation Output Q Contents enqueue(5) -- [5] enqueue(3) [5, 3] dequeue() 5 [3] enqueue(7) [3, 7] 3 [7] front() 7 [] “error” enqueue(9) [9] isEmpty() false size() 1 9 Queues

Applications of Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures Queues

Array-based Queue Can be implemented using an array, Q, of fixed capacity. Use an array of size N in a circular fashion. We can let Q[0] be the front of the queue, however this is inefficient since each dequeue() operation would result in moving all remaining elements forward. That is, each dequeue() operation would have a complexity of O(n). Queues

Array-based Queue Instead, two variables keep track of the front and rear: f index of the front element r index of the next available element in the array (that is the one immediately past the rear/last element) Initially, we assign f = r = 0, which indicates that the queue is empty (generally f = r indicates empty queue). Index r is kept empty. Insertion is made into Q[r] then r is incremented. Queues

Array-based Queue This configuration would allow enqueue(), dequeue() and front() to be performed in constant time, that is O(1). However, this configuration has a serious problem. For example, if we repeatedly enqueue and dequeue a single element N times, then we end up with assign f = r = N. While the array clearly has many empty elements, an insertion would result in array-out-of-bounds error. Queues

wrapped-around configuration Array-based Queue Instead of having this normal configuration, we can let the indices f and r wrap around the end of the array. That is view the array as a “circular array” that goes from Q[0] to Q[N - 1] then back to Q[0]. Notice that Index r is kept empty, which means that the queue can hold a maximum of N-1 elements. normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r Queues

Queue Operations We use the modulo operator (remainder of division) Algorithm size() return ((N - f) + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r Queues

Queue Operations (cont.) Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Operation enqueue throws an exception if the array is full This exception is implementation-dependent Q 1 2 r f Q 1 2 f r Queues

Queue Operations (cont.) Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Q 1 2 r f Q 1 2 f r Queues

Queue Interface Java interface corresponding to our Queue ADT (Note: not identical to the shown interface) Requires the definition of class EmptyQueueException There is no corresponding built-in Java class public interface Queue<E> { public int size(); public boolean isEmpty(); public E front() throws EmptyQueueException; public void enqueue(E element); public E dequeue() throws EmptyQueueException; } Queues

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) Queue Dequeue Enqueue Shared Service Queues

Growable Array-based Queue Queues 4/6/2019 1:36 AM Growable Array-based Queue In an enqueue() operation, when the array is full, we can replace the array with a larger one instead of throwing an exception. Similar to what have been explained for growable array-based stack, the enqueue() operation has amortized running time of: O(n) with the incremental strategy O(1) with the doubling strategy Queues

Queues 4/6/2019 1:36 AM Double-Ended Queues A double-ended queue (dequeue, or D.Q.) ADT is richer than the stack ADT and the queue ADT. It supports insertion and deletion at both ends. Elements can only be added to or removed from the front (head) or the back (tail). Queues

Queues 4/6/2019 1:36 AM Double-Ended Queues The fundamentals operations allowed by such queue are: addFirst(e): Insert as new element at the head of the queue. addLast(e): Insert as new element at the tail of the queue. removeFirst(): Remove and return the first element of the D.Q., or an error if the queue is empty. removeLast(): Remove and return the last element of the D.Q., or an error if the queue is empty. Other operations may include: getFirst(), getLast(), size() and isEmpty() Queues

Example Operation Output D.Q. Contents addFirst(3) -- [3] addFirst(5) [5, 3] removeFirst() 5 addLast(7) [3, 7] addLast(9) [3, 7,9] addLast(12) [3, 7,9, 12] addFirst(8) [8,3, 7,9, 12] removeLast() 12 [8,3, 7,9] 8 isEmpty() false addFirst(6) [6, 3, 7,9] size() 4 Queues