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,
CHAPTER 7 Queues.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
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,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queue Overview Queue ADT Basic operations of queue
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object 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.
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.
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.
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.
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
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
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.
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.
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,
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.
Double-Ended Queues Chapter 5.
Queues Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
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(); bool isEmpty(); Object front() throws QueueEmptyException; void enqueue(Object obj); Object dequeue() throws 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

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 next trailer Object2 prev next Object1 prev next header

Exercises Implement a queue using two stacks. Implement a stack using two queues.