Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?

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.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
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.
CHAPTER 7 Queues.
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 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?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object 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.
Stacks.
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.
© 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
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
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.
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
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.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
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.
Queues Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
Queue data structure.
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
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Queues

What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?

Queue ADT Operations enqueue(o): Insert o at rear of queue –Input: Object; Output: None dequeue(): Remove object at front; error if empty –Input: None; Output: Object removed size(): Return number of objects in queue –Input: None; Output: Integer isEmpty(): Return a boolean indicating queue empty –Input: None; Output: Boolean first(): Return object at front without removing; error if empty –Input: None; Output: Object

Example enqueue(5) enqueue(3) dequeue() enqueue(7) dequeue() front() dequeue() isEmpty() enqueue(9) enqueue(7) size() enqueue(3) enqueue(5) dequeue()

Queue Interface int size() const; bool isEmpty() const; Object& front() throw (QueueEmptyException); void enqueue(const Object& obj); Object dequeue() throw(QueueEmptyException);

Underlying Representation Array versus Linked List –Pros and cons? Running time? –size –isEmpty –enqueue –dequeue –front

Array Implementation …n … enqueue(5) enqueue(3) dequeue() ?

Array Implementation …n … enqueue(5) enqueue(3) dequeue() ? …n-1

Circular Array f – stores index of cell which stores first element of queue r – stores index of next available cell in queue Initially, f = r = 0 How do you add a new element? How do you remove an element? …n-1 fr

Circular Array How do you add a new element? –insert at array[r] –increment r How do you remove an element? –return array[f] –increment f What happens when r >= n-1? …n-1 fr

Circular Array Need to be able to wrap around Modulo – % –increment f using (f+1)%n –increment r using (r+1)%n …n-1 fr

Circular Array 012 f r 012 f r 012 f r 012 f r 012 f enqueue dequeue enqueue r =(2+1)%3= 0

Algorithms size –return (N-f+r) mod N isEmpty –return (f == r) front –if isEmpty then throw QueueEmptyException –return array[f] dequeue –if isEmpty then throw QueueEmptyException –temp = array[f] –f = (f+1)%N –return temp enqueue –if size == N-1 then throw QueueFullException SIZE MUST BE < N-1 –array[r] = object –r = (r+1)%N

Linked List Implementation enqueue dequeue Ø head tail

Linked List Implementation enqueue –if(size == 0) head = new_node –else tail->next_ptr = new_node –tail = new_node –increment size Object next_ptr new_nodehead tail

Linked List Implementation enqueue –if(size == 0) head = new_node –else tail->next_ptr = new_node –tail = new_node –increment size Object1 next_ptr new_nodehead tail Object2 next_ptr

Linked List Implementation dequeue –if (isEmpty) throw QueueEmptyException –head = head->next_ptr –decrement size –if new size is 0 – set tail to NULL

Deque Double-ended queue –insertFirst –insertLast –removeFirst –removeLast –first –last –size –isEmpty

Example insertFirst(3) insertFirst(5) first() removeFirst() insertLast(7) last() removeFirst() removeLast()

Doubly Linked List Algorithms –insertFirst –removeLast Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header