Queue.

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,
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.
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?
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.
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.
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.
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.
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.
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.
CSE 373: Data Structures and Algorithms
Briana B. Morrison Adapted from Alan Eugenio
Queues Queues Queues.
Queues Mohammad Asad Abbasi Lecture 5
Arrays, Stacks, and 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.
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
Stacks and Queues DSA 2013 Stacks n Queues.
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:

queue

Avoid confusion

Britain Italy

Applications of Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Simulations Indirect applications Auxiliary data type for algorithms Component of other data types ADS Lecture 11

The Queue ADT (GoTa §5.2) Auxiliary queue operations: Exceptions Queues 11/19/2018 7:59 AM The Queue ADT (GoTa §5.2) 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 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 on front of an empty queue throws an EmptyQueueException ADS Lecture 11

Queue interface in Java contd. /** * Inspects the element at the front of the queue * @return element at the front of the queue * @exception EmptyQueueException if the queue is empty */ public E front() throws EmptyQueueException; * Inserts an element at the rear of the queue * @param element new element to be inserted public void enqueue (E element); * Removes the element at the front of the queue * @return element removed public E dequeue() throws EmptyQueueException; } ADS Lecture 11

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<E> { /** * Returns the number of elements in the queue * @return number of elements in the queue */ public int size(); * Returns whether the queue is empty * @return true if queue is empty, false otherwise public boolean isEmpty(); ADS Lecture 11

A naïve implementation We could have a one dimensional array Q An integer pointing to the front of the queue and integer pointing to the end of the queue Q.enqueue(x) would do this increment end Q[end] = x Need also Q.peek(), Q.size(), Q.isEmpty(), Q.isFull() Q.dequeue() would do this discuss!!!

A naïve implementation We could have a one dimensional array Q Q.dequeue() would do this discuss!!! result = Q[front]; for (int i=front+1;i<=end;i++) Q[i-1] = Q[i]; end--; Obviously with tests for empty queue etc. What is the complexity of dequeue?

Can use a restricted/disciplined linked list ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

ADS Lecture 11

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 front element r index immediately past the rear element, where we add new elements (enqueue) size is number of entries in the queue

wrapped-around configuration 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 front element r index immediately past the rear element, where we add new elements (enqueue) size is number of entries in the queue normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r ADS Lecture 11 30

Queue Operations We use the modulo operator (remainder of division) 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 Algorithm enqueue(o) if size() = N then throw FullQueueException else Q[r]  o r  (r + 1) mod N Algorithm size() return size Algorithm isEmpty() return size == 0 ADS Lecture 11

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 Pros and cons of array based implementation: Again: quick and easy, all methods run in constant time But again, need good idea of capacity a priori ADS Lecture 11

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 . Dequeue the next element 3 Enqueue the serviced element 2 Service the ADS Lecture 11

Double-Ended queue (deque) Supports insertion & deletion at the front and rear of the queue. Pronounced “deck” Fundamental methods are: addFirst(e) addLast(e) removeFirst() removeLast() Since deque requires insertion & removal at both ends of list, using singly linked list would be inefficient Could use a doubly linked list See GoTa p. 213 ADS Lecture 11

Your mission See assessed exercise 2