Circular Queue in Data Structures: Definition, Operations, and Applications
Table of Content:
Before we start to learn about Circular queue, we should first understand, why we need a circular queue, when we already have linear queue data structure.
In a Linear queue, once the queue is completely full, it's not possible to insert more elements. Even if we dequeue the queue to remove some of the elements, until the queue is reset, no new elements can be inserted. You must be wondering why?
When we dequeue any element to remove it from the queue, we are actually moving the front of the queue forward, thereby reducing the overall size of the queue. And we cannot insert new elements, because the rear pointer is still at the end of the queue.
The only way is to reset the linear queue, for a fresh start.
Circular Queue is also a linear data structure, which follows the principle of FIFO(First In First Out), but instead of ending the queue at the last position, it again starts from the first position after the last, hence making the queue behave like a circular data structure.
Basic features of Circular Queue
- In case of a circular queue,
head
pointer will always point to the front of the queue, andtail
pointer will always point to the end of the queue. - Initially, the head and the tail pointers will be pointing to the same location, this would mean that the queue is empty.
- New data is always added to the location pointed by the
tail
pointer, and once the data is added,tail
pointer is incremented to point to the next available location.
- In a circular queue, data is not actually removed from the queue. Only the
head
pointer is incremented by one position when dequeue is executed. As the queue data is only the data betweenhead
andtail
, hence the data left outside is not a part of the queue anymore, hence removed. - The
head
and thetail
pointer will get reinitialised to 0 every time they reach the end of the queue. - Also, the
head
and thetail
pointers can cross each other. In other words,head
pointer can be greater than thetail
. Sounds odd? This will happen when we dequeue the queue a couple of times and thetail
pointer gets reinitialised upon reaching the end of the queue.
How is a circular queue better than a linear queue?
In a normal queue data structure onces the rear goes to the last index of the queue the queue becomes full, we can not insert the next element until the all elements of queue deleted, even we have some empty cells at the starting end of the array.
Suppose, we have an array of size 5 and index 0,1 and 2 are empty and 3, 4 occupied then we have 0,1 and 2 empty position but cannot insert into queue, it always show the queue is full until all elements are not deleted.
In circular queue we can insert the new element until the all cells of the are not filled. Onces the the rear end reach the index no. 7(as above diag.) it will for index no. 0 if it is empty then we can add.
To check the next position of the rear you can use -
rear = (rear + 1) % 8(size of array).
If rear = 7 and some data we have deleted from the front end then next index of rear would be -
rear + 1 = 7 +1 = 8
rear = 8 % 8 i.e. rear = 0
So , next position of rear would be 0.
Conclusion :
- The memory utilization efficiently in the circular queue.
- The circular queue has less memory consumption as compared to linear queue because while doing insertion after deletion operation it allocates an extra space the first remaining vacant but in the circular queue, the first is used as it comes immediately after the last.
- In a circular queue, the memory of the deleted process can be used by some other new process.
Circular queue is better than a normal queue because in the former we can effectively utilise the memory space. If we have a normal queue and have deleted some elements from there then empty space is created there and even if the queue has empty cells then also we cannot insert any new element because the insertion has to be done from one side only(i.e rear or tail) and deletion has to be done from another side(i.e front or head). But in case of circular queue the front and rear are adjacent to each other.