Circular
Queue
2. Circular
•
Queue
a queue is called circular when the last room comes just before the
first room. That is, Q[0] comes after Q[n-1].
• Also Called as RING BUFFER
• A circular queue is implemented in the same manner as a linear
queue is implemented.
• In simple array implementation of circular queue, the elements are
added circularly.
• It uses 2 variables to keep track of first element and last element.
• Front is used to refer first element and rear is used to refer
last element.
front =rear=-1
Empty Circular Circular Queue with 3
Queue elements
Assume that array
• Condition for “Circular Queue is index starts from
Empty ” 0
FRONT = –1 and REAR=-1
• Condition
FRONT for==
“Circular
(REAR+1)Queue is
% MAX
FULL ”
• These conditions are used to handle Queue Exceptions.
If we try to insert an element into a queue that is already full
then Overflow exception occurs.
If we try to delete an element from an empty queue it will
throw Underflow exception.
Given Circular overflow Exception
Queue
Insert 0 1 2 3
0 20
10 1 30 2 403 4
50 60 10 20 430 40 50
front front rear
rear
Given Circular insertion
0 1 2 3 4 Insert success
Queue 0 1 2 3
20 30 40 50
60
4
60 20 30 40 50
front rear
rear front
Given Circular Queue overflow
0 1 2 3 0Exception
1 2 3 4 FRONT == (REAR+1) %
insert MAX
60 20 430 40 50 60 20 30 40 50
33 Here,
Front=
rear front 1
rear front
Rear=0
Example : Circular Queue using an
array
Initially front=rear= -1. It Circular Queue with 3
indicates
elements
queue is empty.
EMPTY QUEUE
front=rear=-1
Circular Queue after deleting Circular Queue after inserting 30 &
5 40
Operation on Circular
• Queue
Insertion on Circular Queue:
• Steps to do Insertion on Circular Queue
1. If FRONT == (REAR+1) % MAX, then the
circular queue is full. If Circular queue is full
then throw overflow exception.
2. If rear != MAX – 1, then rear will be
incremented by 1 and the new value will be
inserted there.
3. If front != 0 and rear = MAX – 1, then it means that
the queue is not full. So, set rear = 0 and insert the
new element there.
Example: Insert Operation On Circular
Queue
Case -1 : when Circular Queue is Value to be inserted : 22
full
Given Circular
Queue
Insertion Failed. Queue is FULL. Overflow Exception
Case -2 : when rear end has not reached the Maximum
Given Circular
Capacity Value to be
Queue
inserted :
99
Circular Queue after inserting
99
Case -3 : when Circular Queue has space before the front
end Value to be
Given Circular inserted : 99
Queue
Circular Queue after inserting
99
Algorithm to insert new value into a Circular
Queue
Step IF FRONT == (Rear+1) %
1: MAX
print
“OVERFLOW”
Step IF Goto Step-4
FRONT = -1 and REAR
2: [END OF
= -1
IF] SET FRONT =
REAR = 0
ELS
E SET REAR = (Rear+1) %
MAX SET queue[REAR] =
Step value EXIT
3:
Step
4:
• Deletion on Circular Queue:
• Steps to do Deletion on Circular
1. Queue
If = –1, then there are no in the
front
elements(empty
queue Queue). So, throw an underflow
exception.
2. If the queue is not empty and front = rear, then after
deleting the element at the front the queue becomes
empty and so front and rear are set to –1.
3. If the queue is not empty and front = MAX–1, then after
deleting the element at the front, front is set to 0.
4. If the queue is not empty and above 3 conditions are not
met then increment the front by 1.
Example: Delete Operation On Circular
Queue
Case -1 : when Circular Queue is Empty, FRONT
== -1
Deletion Failed. Queue is EMPTY. Underflow
Exception
Case -2 : when Circular Queue is with Single Element,
FRONT==REAR
Given Circular Queue
Circular Queue after
Deletion
Case -3 : when FRONT reached the maximum capacity, FRONT==MAX-
1
Given Circular
Queue
Circular Queue after
Deletion
Case -4 : when previous 3 cases are not met.
Circular Queue is not empty, front did not reach
maximum
capacity and there are more than one element in the
queue
Given Circular Queue
Circular Queue after
Deletion
Algorithm to delete a value from a Circular
Queue
Step IF FRONT ==-1
1: print
“UNDERFLOW”
Goto Step-
Step 4 [END OF IF]
2: IF FRONT
SET ==
value
REAR
= //case-2: q with single
Step queue[FRONT]
element
SET FRONT = REAR = -1
3: ELSE
FRONT = (FRONT+1) % MAX
Sep 4: EXIT
Example : Operations on Circular
Queue
i) Initially front=rear= -1. and queue is
empty.
0 1 2 3 4
front=rear=-1
ii) Insert 10 iii) Insert
0 1 2 3 20
0 1 2 3 4
10 4 10 20
front rear front rear
iv) Insert 30 v) Insert
0 1 2 3 400 1 2 3 4
10 4 30 10 20 30 40
20
front rear front rear
vi) (10 is vii) Insert 60
Delete removed)
0 201 0 1 2 3
30
2 40
3 4 20 430 40 60
front rear front rear
viii) Insert ix) Insert 33(overflow
99 Exception)
0 1 2 3 4 0 1 2 3 4
99 20 30 40 50 99 20 30 40 50
rear front rear front
x) Delete ( 20 is xi) Delete ( 30 is
removed)
0 1 2 3 4 removed)
0 1 2 3 4
99 30 40 50 99 40 50
rear front rear front
xii) Delete ( 40 is
xiii) Delete ( 50 is
removed)
0 1 2 3 4 removed)
0 1 2 3 4
99 50
99
rear front rear front
xiv) Delete ( 99 is
removed)0 1 2 3 4
Front = rear = -1 Circular Queue became
EMPTY
Algorithm to display the elements of a Circular
Queue
Step IF FRONT ==-1 and REAR == -
1: 1
print “Queue is
empty”
Step IF FRONT
Goto< Step-
REAR
2: 4 [ENDREPEA
OF IF]T for i from FRONT to
REAR DO PRINT queue[i]
ELSE
REPEAT for i from FRONT to
MAX DO PRINT queue[i]
REPEAT for i from 0 to
REAR DO PRINT
queue[i]
[END OF
Step IF] EXIT
4:
Example-1: Given Circular Display the Content of the
Queue0 1 2 3 4 Queue
99 30 40 50 30,40,50,99
rear front
Example-2: Given Circular
Queue Display the Content of the
0 20 301 402 603 4 Queue
20,30,40,60
front rear
Example-3: Given Circular
Queue Display the Content of the
0 1 2 3 4 Queue
99 20 30 40 50
20,30,40,50,99
rear front