Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.

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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
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.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
© 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.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
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.
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.
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.
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.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Stacks and Queues MR. Mohammad Alqahtani.
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,
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.
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.
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.
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
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid

Definition: a Queue is a collection of objects that are inserted and removed according to the first-in-first-out (FIFI – LILO) principle. That is, elements can be inserted at any time, but only the element that has been in the queue the longest (first inserted) can be removed at any time. Usually, elements enter a queue at the Rear, and are accessed or removed from the Front. Queues

Queues The Queue ADT (§5.2.1) 4/14/2017 5:24 PM The Queue ADT stores a sequence of arbitrary objects. Insertions and deletions follow the first-in first-out scheme. Insertions are restricted to the end of the sequence (Rear). Accesses and removals are restricted to the first element of the sequence (Front). Queues Dr Zeinab Eid

Main queue operations: The queue abstract data type (ADT) supports the following methods (operations): Main queue operations: enqueue(e): inserts an element e at the end of the queue dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: front(): returns the element at the front without removing it size(): returns an integer value that indicates the number of elements stored in the queue isEmpty(): returns a Boolean value that indicates whether the queue is empty Exceptions: Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues

Queue Example: The following table shows a series of queue operations and their effects on an initially empty queue Q of integer objects: Operation Output Front Q Rear enqueue(5) - (5) enqueue(3) (5,3) dequeue() 5 (3) enqueue(7) (3,7) 3 (7) front() 7 () “error” isEmpty() true enqueue(9) (9) enqueue(8) (9,8) Size() 2 (9,8,3) (9,8,3,5 ) 9 (8,7,3) Queues

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

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

5.2.2 Array-Based Queue Implementation a- Simple Array Implementation Using an array Q, of fixed size N, to store queue elements. If we let Front is Q[0], and let queue grow from left to right, as in case of Stack, it’s not efficient solution. It requires moving all queue-elements forward one-array cell, each time we perform dequeue operation. Such an implementation requires O(n) time to perform dequeue-method, where n is the current elements in queue. Queues

b- Using an Array in a Circular Way To avoid moving objects once they are placed in Q, and to execute queue methods in a constant time O(1) Use an array of fixed size N in a circular fashion Define two integer variables to keep track of the front and rear array cells f index of the front element in Q, first nonempty cell of array r index to the next available empty array cell in Q, rear element Array location r is kept empty normal configuration f<r Q 1 2 r f N-1 wrapped-around configuration f>r Q 1 2 f r N-1 Queues

Queue Operations Q 1 2 r f Q 1 2 f r Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Algorithm Front() if isEmpty() then throw EmptyQueueException else return Q[f] Initially, we assign f = r =0, which indicates that the queue is empty. We use the modulo operator % (remainder of division) Q 1 2 r f N-1 Q 1 2 f r N-1 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 N-1 Q 1 2 f r N-1 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 N-1 Q 1 2 f r N-1 Queues

Queue Operations (cont.) Method and Time Size O(1) isEmpty O(1) Front O(1) Enqueue O(1) Dequeue O(1) Disadvantage of the array-based queue implementation: fixed capacity value.

5.2.3 Implementing Queue with Linked Lists we choose the front of the queue to be at the head of the list the rear of the queue to be at the tail of the list. we remove from the head and insert at the tail. Queues

5.2.3 Implementing Queue with Linked Lists Queues

5.2.3 Implementing Queue with Linked Lists Each method of the singly linked list implementation of the queue ADT runs in O(1) time. We avoid the need to specify a maximum size for the queue but this benefit comes at the expense of increasing the amount of space used per element. We must take care with special cases where the queue is empty before an enqueue where the queue becomes empty after a dequeue.   Queues

Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements Linked Lists

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) The Queue Shared Service 1 . Deque the next element 3 Enqueue the serviced element 2 Service the Queues

Double-Ended Queues (deque)( "D.Q.") Supports insertion and deletion at both the front and the rear of the queue. The Deque Abstract Data Type  addFirst(e): Insert a new element e at the beginning of the deque.  addLast(e): Insert a new element e at the end of the deque.  removeFirst(): Remove and return the first element of the deque; an error occurs if the deque is empty. removeLast(): Remove and return the last element of the deque; an error occurs if the deque is empty. getFirst(): Return the first element of the deque; an error occurs if the deque is empty. getLast(): Return the last element of the deque; an error occurs if the deque is empty. size(): Return the number of elements of the deque. isEmpty(): Determine if the deque is empty. Queues

Double-Ended Queues (cont.) Operation Output D addFirst(3) - (3) addFirst(5) - (5,3) removeFirst() 5 (3) addLast(7) - (3,7) removeFirst() 3 (7) removeLast() 7 () removeFirst() "error" () isEmpty() true () Queues

Implementing a Deque Doubly linked list: Since the deque requires insertion and removal at both ends of a list, using a singly linked list to implement a deque would be inefficient. Method Time size, isEmpty O(1) getFirst, getLast O(1) add First, addLast O(1) removeFirst, removeLast O(1) Queues

Queues

Queues

Queues