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.

Slides:



Advertisements
Similar presentations
Queues and Linked Lists
Advertisements

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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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,
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
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 and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
© 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,
Data Structures Using C, 2e
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.
Double-Ended Queues Chapter 5.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Stacks, Queues, and Deques
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.
DATA STRUCTURE QUEUE.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Revised based on textbook author’s notes.
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.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Stacks, Queues, and Deques
Stacks and Linked Lists
Data Structures & Programming
Data Structures & Programming
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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 end, deletion at the other Enqueue () – Insert element in the queue at the rear position Dequeue () – Delete element from the front position of the queue

The Queue ADT enqueue(e): Insert element e at the rear of the queue dequeue(): Remove element at the front of the queue; an error occurs if the queue is empty. front(): Return, but do not remove, a reference to the front element in the queue; an error occurs if the queue is empty. size(): Return the number of elements in the queue. empty(): Return true if the queue is empty and false otherwise.

Queues using Arrays e1e2e3e4e5 front rear e1e2e3e4e5e6e7e8e9e10 frontrear Wrongly indicates that the queue is full, when there is empty space in the front

Queues using Arrays – Circular Implementation e11e12e3e4e5e6e7e8e9e10 frontrear e11e12e13e14e15e16e17e18e9e10 front rear

Circular Queue front rear A B CD E F G H

Circular Queue using Arrays MaxSize:- Maximum Size of the Array – How will the elements be stored in the array or how will the index vary? 0 to MaxSize – 1 n:- current number of elements in the array – How will the variable n vary? 0 to MaxSize; 0 will indicate Empty; MaxSize will indicate Full

Circular Queue using Arrays rear:- points to the last element of the array; – needs to be updated before enqueue front: points to the first element of the array; – needs to be updated after dequeue What can be the initial value of rear & front? – Both can be initialize to -1

Circular Queue using Arrays How do we check for an empty queue? If (n=0) then “Queue Empty” How do we check for an full queue? If (n=Maxsize) then “Queue Full”

Queues using Arrays - enQueue 1.Check if there is space to Insert i.e. Queue is not full 2.Since rear points to the last element, update rear, considering wrap around 3.Enqueue the element 4.If it is the first element, update front also 5.Update the number of elements in the queue

Queues using Arrays - enQueue If (n== MaxSize) then {“Queue Full”; return } rear = rear + 1 If (rear == MaxSize) then rear = 0 queue[rear] = data If (n == 0) then { front = front + 1 } n=n+1

Queues using Arrays - deQueue 1.If Queue is empty, then return 2.deQueue the first element 3.Update number of elements 4.If the queue becomes empty, 1.Update both front and rear 2.Otherwise update front, considering wrap around

Queues using Arrays-deQueue If (n=0) then {“Queue Empty” ;return } data = queue[front] n= n-1 if (n == 0) then {front = -1; rear = -1} Else { front = front + 1; if (front = MaxSize) then front = 0}

Queues using arrays Case Queue (Maxsize = 3) nfrontrear A B- 4--C 5 AB- AB- 6 -BC -BC 7 A-C C-C 8 ABC ABC

Queues using arrays Case Queue (Maxsize = 3) nfrontrear A B C122 5 AB-201 AB BC212 -BC221 7 A-C202 C-C220 8 ABC302 ABC320

Circular Queues / Mod Function When the Max Size of Queue is 3 Value of front and rear varies from – -1,0,1,2 If you divide any number by 3, the remainder will be 0,1 or 2

Contd. Empty Queue: n=0; Initial value of front and rear = 0 Enqueue – Increment n (n++) – Increment r [(r+1) mod N] Dequeue – Decrement n (i.e. n--); – Increment f [(f+1) mod N]

Queues using Arrays – alternate implementation n is the current number of elements in the queue. front is the index of the cell of Queue storing the front of the queue. – If the queue is nonempty, this is the index of the element to be removed by dequeue. rear is an index of the cell of Q following the rear of the queue. – If the queue is not full, this is the index where the element is inserted by enqueue.

Queues using Arrays – alternate implementation size() { return n } empty() { return (n == 0) } dequeue() { if empty() then “Queue Empty” data = q[f] f =( f +1) mod N; n = n−1 } enqueue(e): { if size() = = N then “Queue Full” Q[r]= e; r=(r+1) mod N; n = n+1 }

To Do How would you implement a circular queue using circular linked list?

DeQue (“Deck”) Double Ended Queue A queue-like data structure that supports insertion and deletion at both the front and rear of the queue

The Deque ADT insertFront(e) insertBack(e) eraseFront() eraseBack() front() back() size() empty()

To Do How will you implement a Deque with a Doubly Linked List?

Reference Goodrich et.al.(2011)