Circular queue data structure

Before we understand what is circular queue and why are we implementing circular queue, we must first understand-   drawbacks of linear queue data structure .

What is Circular queue?
A circular queue is a slight variation of a linear queue data structure where where we can reset front and rear to zero'th index after they are at the upper bound. In this way front and rear will move in cyclic order and the queue becomes circular queue.
The advantage is that if the space is present then code will never display 'queue overflow' but we have to change in the deletion(dequeue) and insertion(enqueue) function.
Example:-
Note:- Initially, both front and rear points to -1 which indicates that the queue is empty.

Pseudocode for insertion(enqueue) in a Circular queue:- 
Enqueue operation of a circular queue involves a series of steps-
step 1- Check whether the queue is full or not.
step 2- If queue is full then print the message "queue overflow" and return.
step 3- 
If queue is not full then reset rear.
step 4- Place the element in the queue at the position where rear is pointing.
step 5- If it is first element insertion then set front at zero'th index position.

step 6- Finish and return.

Pseudocode for deletion(dequeue) in a Circular queue:- 
Dequeue operation of a circular queue involves a series of steps-
step 1- Check whether the queue is empty or not.
step 2- If queue is empty then print the message "queue underflow" and return.
step 3- If queue is not empty then remove the element from the queue at which front is pointing.
step 4- Reset front and if required then rear also reset.
step 5- Return the deleted element.


Important interview questions regarding Circular queue:- 
Q. When will be overflow in a circular queue?
Ans:- when value of rear is greater then or equal to size-1 of circular queue and front points to zero'th index position then circular queue will be an state of overflow.

Q. When will be underflow in a circular queue?
Ans:- when front points -1 index then circular queue will be an state of underflow.

Q. How to increment or reset front and rear in a circular queue?
Ans:- for example size of circular queue is 5, if front or rear will be at 0 to 3rd index position then it will be increment by ++1 and if front or rear at 4th(last) index position then it will not be increment by ++1, it will be reset to zero index position.

Important rule of the circular queue:- 
Important rule of circular queue is that, whenever front and rear points at the same index position then that element will be the last element of the circular queue.

Implementation of circular queue in c language:-
Output:-

Implementation of circular queue in Java language:-
Output:-

also see:-
Data structure: classification of data structure
Stack data structure
Queue: types of queue
Linear or simple queue
Drawbacks of linear queue
Searching techniques in data structure

Post a Comment

0 Comments