Queues.

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
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?
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. … 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.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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)
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.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
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.
© 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.
Queues Rem Collier Room A1.02
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.
Queues.
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,
ADT list.
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
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) front() isEmpty() 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 1 2 3 4 5 6 … n-1 enqueue(5) enqueue(3) 5 3 1 2 3 1 2 3 4 5 6 … n-1 enqueue(5) enqueue(3) 5 3 1 2 3 4 5 6 … n-1 dequeue() ?

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

Circular Array f r 1 2 3 4 5 6 … n-1 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?

Circular Array How do you add a new element? f r 1 2 3 4 5 6 … n-1 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?

Circular Array Need to be able to wrap around Modulo – % f 1 2 3 4 5 6 … n-1 Need to be able to wrap around Modulo – % increment f using (f+1)%n increment r using (r+1)%n

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

Algorithms size isEmpty front dequeue enqueue return (N-f+r) mod N return (f == r) front if isEmpty then throw QueueEmptyException return array[f] dequeue 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 tail head Ø enqueue dequeue

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

Linked List Implementation new_node head tail Object1 Object2 enqueue if(size == 0) head = new_node else tail->next_ptr = new_node tail = new_node increment size next_ptr 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

Exercises Implement a queue using two stacks Implement a two-color stack