Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

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

© 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.
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.
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.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority 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.
Queues What is a Queue? Queue Implementations: Queue As Array
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.
© 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.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture6: Stacks 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  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
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.
© 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.
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.
Queues Rem Collier Room A1.02
Stacks.
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
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.
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
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues What is a Queue? Queue Implementations: As Array
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Lecture 8: Stacks, Queues
Queues What is a queue? Queue Implementations: As Array
Stacks and Linked Lists
Presentation transcript:

Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue What is queue?  The Queue ADT stores arbitrary objects.  An ordered list in which insertions and deletions are made at different sides, called rear and front, respectively. Properties  First‐In‐First‐Out (FIFO) scheme  Typically implemented with array or linked list Queue class in Java  2

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Operations Main operations:  enqueue(Object) : inserts an element at the “rear” of the queue  Object dequeue() : removes and returns the element at the “front” of the queue Others:  Object peek() : returns the element at the “front” without removing it  int size() : returns the number of elements stored  boolean isEmpty() : indicates whether no elements are stored 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 4 OperationOutputQueue enqueue(5) enqueue(3) dequeue() enqueue(7) dequeue() peek() dequeue() isEmpty() enqueue(9) enqueue(7) size() enqueue(3) enqueue(5) dequeue() – 5 – 3 7 “exception” true – 2 – 9 [5] [5, 3] [3] [3, 7] [7] [] [9] [9, 7] [9, 7, 3] [9, 7, 3, 5] [7, 3, 5]

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Queue 5 Q 012rf

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Queue 6 Q 012rf normal configuration Q 012fr wrapped-around configuration Q 012rf Q 012rf No more insertions! Empty

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Interface in Java Restrictions  The execution of dequeue() and peek() on an empty queue throws an EmptyQueueException().  The execution of enqueue() on a full queue throws an FullQueueException(). 7 public interface Queue { public int size(); public boolean isEmpty(); public Object peek() throws EmptyQueueException(); public void enqueue(Object element) throws FullQueueException(); public Object dequeue() throws EmptyQueueException(); }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Operations Implementation of a circular array: modulo operator  size() function  isEmpty() function 8 Algorithm size() return (N - f + r) mod N Q 012rf Q 012fr Algorithm isEmpty() return (f = r) Q 012r=f if f ≤ r then return r ‐ f else return N ‐ (f ‐ r) return (size()==0)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Operations Element insertion  enqueue() : add an element at rear  The array storing the queue elements may become full. Front and rear indicate the same element in the queue array. An enqueue operation will then throw a FullQueueException(). Limitation of the array‐based implementation 9 Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 012rf Q 012fr

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Implementation in Java 10 public class BoundedQueue implements Queue { private Object[] array; private int size = 0; private int front = 0; // index of the current front item private int rear = 0; // index of next item to be added public BoundedQueue(int capacity) { array = new Object[capacity]; } public boolean isEmpty() { return (size == 0); } public int size() { return size; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Implementation in Java 11 public void enqueue(Object item) { if (size == array.length) throw new FullQueueException(); array[rear] = item; rear = (rear + 1) % array.length; size++; } public Object dequeue() { if (size == 0) throw new EmptyQueueException(); Object item = array[front]; array[front] = null; front = (front + 1) % array.length; size‐‐; return item; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Queue 12

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Based on Linked List 13 public class LinkedQueue implements Queue { private class Node { public Object data; public Node next; public Node(Object data, Node next) { this.data = data; this.next = next; } private Node front = null; private Node rear = null; public boolean isEmpty() { return (front == null); }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Based on Linked List 14 public int size() { int count = 0; for (Node node = front; node != null; node = node.next) count++; return count; } public void enqueue(Object item) { Node newNode = new Node(item, null); if (isEmpty()) front = newNode; else rear.next = newNode; rear = newNode; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue Based on Linked List 15 public Object dequeue() { if (isEmpty()) throw new EmptyQueueException(); Object item = front.data; if (rear == front) rear = null; front = front.next; return item; } public Object peek() { if (front == null) throw new EmptyQueueException(); return front.data; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Applications of Queue Direct applications  Waiting lists  Access to shared resources (e.g., printer)  Multiprogramming Indirect applications  Auxiliary data structure for algorithms  Component of other data structures 16

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps:  e = Q.dequeue();  doService(e);  Q.enqueue(e); 17 Shared Service Queue

18