1 Queues (5.2) CSE 2011 Winter 2011 4 May 2015. Announcements York Programming Contest https://wiki.cse.yorku.ca/project/ACM/ Link also available from.

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
Data Structures ADT List
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,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
© 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?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
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.
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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  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.
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.
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,
Elementary Data Structures
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.
Linked List Stacks, Linked List Queues, Dequeues
Stacks and Queues.
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Stacks and Linked Lists
Presentation transcript:

1 Queues (5.2) CSE 2011 Winter May 2015

Announcements York Programming Contest Link also available from the “News and Lecture Notes” page Midterm test: tentative date March 1, 13:00-14:20 (80 minutes) 2

3 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: – Insertions: at the rear of the queue – Removals: at the front of the queue Applications, examples: – Waiting lists – Access to shared resources (e.g., printer) – Multiprogramming (UNIX)

4 Queue ADT Data stored: arbitrary objects 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 – object front(): returns the element at the front without removing it Execution of dequeue() or front() on an empty queue → throws EmptyQueueException Another useful operation: – boolean isEmpty(): returns true if the queue is empty; false otherwise.

5 Queue Operations enqueue(object) object dequeue() object front() boolean isEmpty() int size(): returns the number of elements in the queue Any others? Depending on implementation and/or applications public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public Object dequeue() throws EmptyQueueException; public void enqueue (Object obj); }

Queues6 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 )

Array-based Implementation An array Q of maximum size N We need to decide where the front and rear are. How to enqueue, dequeue? Running time of enqueue? Running time of dequeue? 7

8 Array-based Implementation (2) 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 past the rear element Note: Q[r] is empty (does not store any object)

9 Array-based Implementation (3) Front element: Q[f] Rear element: Q[r – 1] Queue is empty: f = r Queue size: r – f How to dequeue? How to enqueue?

10 Dequeue() and Enqueue() Algorithm dequeue(): if (isEmpty()) throw QueueEmptyException; temp = Q[f]; f = f + 1; return temp; Algorithm enqueue(object): if (r == N) throw QueueFullException; Q[r] = object; r = r + 1; What if r == N and Q[0…3] cells are empty?

11 Circular Array Implementation Analogy: A snake chases its tail Front element: Q[f] Rear element: Q[r – 1] Incrementing f, r f = (f + 1) mod N r = (r + 1) mod N mod: Java operator “%”

12 Circular Array Implementation (2) Queue size = (N – f + r) mod N → verify this Queue is empty: f = r When r reaches and overlaps with f, the queue is full: r = f To distinguish between empty and full states, we impose a constraint: Q can hold at most N – 1 objects (one cell is wasted). So r never overlaps with f, except when the queue is empty.

13 Pseudo-code Algorithm enqueue(object): if (size() == N – 1) throw QueueFullException; Q[r] = object; r = (r + 1) mod N; Algorithm dequeue(): if (isEmpty()) throw QueueEmptyException; temp = Q[f]; f = (f + 1) mod N; return temp;

14 Pseudo-code (2) Algorithm front(): if (isEmpty()) throw QueueEmptyException; return Q[f]; Algorithm isEmpty(): return (f = r); Algorithm size(): return ((N – f + r) mod N); Homework: Remove the constraint “Q can hold at most N – 1 objects”. That is, Q can store up to N objects. Implement the Queue ADT using a circular array. Note: there is no corresponding built-in Java class for queue ADT

15 Analysis of Circular Array Implementation Performance Each operation runs in O(1) time Limitation The maximum size N of the queue is fixed How to determine N? Alternatives?  Extendable arrays  Linked lists (singly or doubly linked???)

16 Singly or Doubly Linked? Singly linked list public static class Node { private Object data; private Node next; } Needs less space. Simpler code in some cases. Insertion at tail takes O(n). Doubly linked list public static class DNode { private Object data; private Node prev; private Node next; } Better running time in many cases (discussed before).

17 Implementing a Queue with a Singly Linked List Head of the list = front of the queue (enqueue) Tail of the list = rear of the queue (dequeue) Is this efficient?

18 dequeue(): Removing at the Head Running time = ?

19 enqueue(): Inserting at the Tail Running time = ?

20 Method enqueue() in Java public void enqueue(Object obj) { Node node = new Node(); node.setElement(obj); node.setNext(null); // node will be new tail node if (size == 0) head = node; // special case of a previously empty queue else tail.setNext(node); // add node at the tail of the list tail = node; // update the reference to the tail node size++; }

21 Method dequeue() in Java public Object dequeue() throws QueueEmptyException { Object obj; if (size == 0) throw new QueueEmptyException("Queue is empty."); obj = head.getElement(); head = head.getNext(); size––; if (size == 0) tail = null; // the queue is now empty return obj; }

22 Analysis of Implementation with Singly- Linked Lists Each methods runs in O(1) time Note: Removing at the tail of a singly-linked list requires θ(n) time. Avoid this! Comparison with array-based implementation: No upper bound on the size of the queue (subject to memory availability) More space used per element (next pointer) Implementation is more complicated (pointer manipulations) Method calls consume time (setNext, getNext, etc.)

Homework In the doubly linked list implementation, we use two dummy nodes, header and trailer, to make coding simple. Why don’t we use two dummy nodes in the singly linked list implementation? Hint: Implement the queue operations using a singly linked list with two dummy nodes header and trailer.

24 Next time … Double-ended Queues (Deques) (5.3) Extendable Arrays Solutions to Assignment 1  Discussed in class  No solutions will be posted.