Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Circular Queues Maitrayee Mukerji

2 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

3 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.

4 Queues using Arrays e1e2e3e4e5 front rear e1e2e3e4e5e6e7e8e9e10 frontrear Wrongly indicates that the queue is full, when there is empty space in the front 012 34 567 89012 34 567 89

5 Queues using Arrays – Circular Implementation e11e12e3e4e5e6e7e8e9e10 frontrear e11e12e13e14e15e16e17e18e9e10 front rear 012 34 567 89012 34 567 89

6 Circular Queue front rear A B CD E F G H

7 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

8 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

9 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”

10 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

11 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

12 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

13 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}

14 Queues using arrays Case Queue (Maxsize = 3) nfrontrear 012 1---0 2A--100 3-B- 4--C 5 AB- AB- 6 -BC -BC 7 A-C C-C 8 ABC ABC

15 Queues using arrays Case Queue (Maxsize = 3) nfrontrear 012 1---0 2A--100 3-B-111 4--C122 5 AB-201 AB-210 6 -BC212 -BC221 7 A-C202 C-C220 8 ABC302 ABC320

16 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

17 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]

18 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.

19 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 }

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

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

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

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

24 Reference Goodrich et.al.(2011)


Download ppt "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."

Similar presentations


Ads by Google