Queues by Dr. Bun Yue Professor of Computer Science 2013 CSCI 3333 Data Structures.

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

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 Briana B. Morrison Adapted from Alan Eugenio.
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.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
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?
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
Stacks and QueuesCSC311: Data Structures1 Chapter 5 Stacks and Queues Objectives –Stacks and implementations –Queues and implementations –Double-ended.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Stacks & Queues EECS: Stacks & Queues Stacks April 23, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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)
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)
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.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
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
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
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.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
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.
1 COMP9024: Data Structures and Algorithms Week Four: Stacks and Queues Hui Wu Session 1, 2016
© 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.
CSCI 3333 Data Structures Stacks.
Csc 2720 Data structure Instructor: Zhuojun Duan
Stacks.
Queues Queues Queues.
Queues Mohammad Asad Abbasi Lecture 5
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.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
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.
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
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures

Acknowledgement  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich

Queues  First In First Out (FIFO) data structure.

Queues  The Queue ADT stores arbitrary objects  Insertions and deletions follow the first-in first-out 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

Queue Operations  Some 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

Queue Example OperationOutputQ enqueue( 5 ) – ( 5 ) enqueue( 3 ) – ( 5, 3 ) dequeue() 5 ( 3 ) enqueue( 7 ) – ( 3, 7 ) dequeue() 3 ( 7 ) front() 7 ( 7 ) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue( 9 ) – ( 9 ) enqueue( 7 ) – ( 9, 7 ) size( )2 ( 9, 7 ) enqueue( 3 ) – ( 9, 7, 3 ) enqueue( 5 ) – ( 9, 7, 3, 5 ) dequeue() 9 ( 7, 3, 5 )

Queue Interface in Java  Not a class!  Super-interfaces: Collection Iterable  Sub-interfaces: BlockingDeque : waits for queue to become non-empty when dequeue. Deque

Known Implementations of Queue  AbstractQueue  ArrayBlockingQueue  ArrayDeque  ConcurrentLinkedQueue  DelayQueue  LinkedBlockingDeque  LinkedBlockingQueue  LinkedList  PriorityBlockingQueue  PriorityQueue  SynchronousQueue

Some Implementation Criteria  As queues are general data structures for widespread use, there are variations to fit different applications. Some considerations: Implementation: array vs. linked list. Thread safe or not. Blocking or not. Synchronous or not: i.e. rendezvous in Ada Allow null elements or not.

Applications of Queues  Some direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Simulation Breadth-first traversals Multiprogramming Messaging  Indirect applications Auxiliary data structure for algorithms Component of other data structures

Array-based Queue  Use an array of size N in a circular fashion  Two variables keep track of the front and rear f index of the front element r index immediately past the rear element  Array location r is kept empty Q 012rf normal configuration Q 012fr wrapped-around configuration

Queue Operations  We use the modulo operator (remainder of division) Algorithm size() return (N  f + r) mod N Algorithm isEmpty() return (f  r) Q 012rf Q 012fr

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 012rf Q 012fr

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

Queue Interface in Java  Java interface corresponding to our Queue ADT  Requires the definition of class EmptyQueueException  No corresponding built-in Java class public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }

Linked List Implementation  We have already implemented methods necessary for the queue ADT (under different method names) using a singly linked list.

Application: Round Robin Schedulers  We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. e = Q.dequeue() 2. Service element e 3. Q.enqueue(e) The Queue Shared Service 1.Deque the next element 3.Enqueue the serviced element 2.Service the next element

Palindrome in C++: Spot the error #include “Stack.h”; #include “Queue.h”; #include using namespace std; int main() {// Incorrect solution. Can you see it? StackS; QueueQ; charch; cout << “Enter string to be tested as palindrome: “; while (cin >> ch) { S.push(ch); Q.enqueue(ch); } bool pal = true; while (!Q.isEmpty()) pal = Q.dequeue() == S.pop(); if (pal) cout << “Palindrome!!” << endl; return EXIT_SUCCESS; }

Example: Queue in Ruby  Ruby is a newer object oriented scripting language using some software engineering principles.  We use a queue example in Ruby to illustrative the use of queues for concurrent processes.  There is no need to understand all details of the example.

Queue.rb require 'thread' queue = Queue.new producer = Thread.new do# concurrent thread 1 5.times do |i| sleep rand(i) # simulate expense queue << i puts "#{i} produced" end consumer = Thread.new do# concurrent thread 2 5.times do |i| value = queue.pop sleep rand(i/2) # simulate expense puts "consumed #{value}" end consumer.join

Executing Queue.rb 0 produced 1 produced consumed 0 2 produced consumed 1 consumed 2 3 produced consumed 3 4 produced consumed 4

Discussion  Similar operations because of similar ADT new << (push): enqueue pop: dequeue  “thread-safe”

Questions and Comments?