Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.

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.
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.
CHAPTER 7 Queues.
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.
Data Structure Dr. Mohamed Khafagy.
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.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
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.
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.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
© 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,
Review Array Array Elements Accessing array elements
Queues.
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.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
Queues What is a queue? Queue Implementations: As Array
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
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.
Queues.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
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,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues CSE 373 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Stacks and Queues CSE 373 Data Structures.
Lecture 8: Stacks, Queues
CSE 373 Data Structures Lecture 6
Queues What is a queue? Queue Implementations: As Array
Lecture 9: Stack and Queue
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues

The Queue ADT The Queue ADT stores arbitrary objects Queues 12/3/2018 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme (FIFO) Insertions are at the rear of the queue and removals are at the front of the queue © 2014 Goodrich, Tamassia, Goldwasser Queues

Main queue operations enqueue(Q, item) // insert in the book Queues 12/3/2018 Main queue operations enqueue(Q, item) // insert in the book inserts an item at the end of the queue item dequeue(Q) // delete in the book removes and returns the item at the front of the queue © 2014 Goodrich, Tamassia, Goldwasser Queues

Auxiliary queue operations Queues 12/3/2018 Auxiliary queue operations item first(Q): returns the element at the front without removing it integer size(Q): returns the number of elements stored boolean isEmpty(Q): indicates whether no elements are stored Boundary cases: Attempting the execution of dequeue or first on an empty queue returns null © 2014 Goodrich, Tamassia, Goldwasser Queues

Example Operation Output Q enqueue(Q,5) – (5) enqueue(Q,3) – (5, 3) dequeue(Q) 5 (3) enqueue(Q,7) – (3, 7) dequeue(Q) 3 (7) first(Q) 7 (7) dequeue(Q) 7 () dequeue(Q) null () isEmpty(Q) true () enqueue(Q,9) – (9) enqueue(Q,7) – (9, 7) size(Q) 2 (9, 7) enqueue(Q,3) – (9, 7, 3) enqueue(Q,5) – (9, 7, 3, 5) dequeue(Q) 9 (7, 3, 5) © 2014 Goodrich, Tamassia, Goldwasser 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 © 2014 Goodrich, Tamassia, Goldwasser Queues

wrapped-around configuration Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and size f index of the front element sz number of stored elements When the queue has fewer than N elements, array location r = (f + sz) mod N is the first empty slot past the rear of the queue normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues

Why “circular” array? Why not “regular” array? the first item on the queue is always the first item in the array? front rear 5 3 9 7 © 2014 Goodrich, Tamassia, Goldwasser Queues

Can we use a linked list to implement a queue? What are the tradeoffs? © 2014 Goodrich, Tamassia, Goldwasser Queues

Queue Operations Algorithm size(Q) return Q->sz Algorithm isEmpty(Q) return (Q->sz == 0) Q 1 2 r f Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues

Queue Operations (cont.) Algorithm enqueue(Q, item) if size(Q) = N then print error else r  (Q->f + Q->sz) mod N Q->data[r]  item Q->sz  (Q->sz + 1) 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 © 2014 Goodrich, Tamassia, Goldwasser Queues

Queue Operations (cont.) Note that operation dequeue returns null if the queue is empty Algorithm dequeue(Q) if isEmpty(Q) then return null else item  Q->data[Q->f] Q->f  (Q->f + 1) mod N Q->sz  (Q->sz - 1) return o Q 1 2 r f Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues

Worst-case time complexity N items on the queue Operation “Circular Array” “Normal” Array Linked List (head & tail) enqueue dequeue © 2014 Goodrich, Tamassia, Goldwasser Queues

Worst-case time complexity N items on the queue Operation “Circular” Array “Normal” Array Linked List (head & tail) enqueue O(1) dequeue O(N) © 2014 Goodrich, Tamassia, Goldwasser Queues

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: e = dequeue(Q) Service item e enqueue(Q, e) Queue Dequeue Enqueue Shared Service © 2014 Goodrich, Tamassia, Goldwasser Queues