A Circular Queue is a queue data structure but circular in shape, therefore after the last position, the next place in the queue is the first position.
We recommend you to first go through the Linear Queue tutorial before the Circular queue, as we will be extending the same implementation.
In the case of Linear queue, we did not have the head and tail pointers because we used python List for implementing it. But in case of a circular queue, as the size of the queue is fixed, hence we will set a maxSize for our list used for queue implementation.
A few points to note here are:
-
In case of a circular queue,
headpointer will always point to the front of the queue, andtailpointer will always point to the end of the queue. -
Initially, the
headand thetailpointers 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
tailpointer, and once the data is added,tailpointer is incremented to point to the next available location.

-
In a circular queue, data is not actually removed from the queue. Only the
headpointer is incremented by one position whendequeueis executed. As the queue data is only the data betweenheadandtail, hence the data left outside is not a part of the queue anymore, hence removed.

- The
headand thetailpointer will get reinitialized to 0 every time they reach the end of the queue.

-
Also, the
headand thetailpointers can cross each other. In other words,headpointer can be greater than thetail. Sounds odd? This will happen when wedequeuethe queue a couple of times and thetailpointer gets reinitialized upon reaching the end of the queue.

-
Another very important point is keeping the value of the
tailand theheadpointer within the maximum queue size. In the diagrams above the queue has a size of 8, hence, the value oftailandheadpointers will always be between 0 and 7. This can be controlled either by checking every time whethertailorheadhave reached themaxSizeand then setting the value 0 or, we have a better way, which is, for a valuexif we divide it by 8, the remained will never be greater than 8, it will always be between 0 and 7, which is exactly what we want.
If you want to see a Queue operating in realtime, check out this animation.
Algorithm for Circular Queue
-
Initialize the queue, with size of the queue defined (
maxSize), andheadandtailpointers. -
enqueue: Check if the number of elements is equal to
maxSize - 1:-
If Yes, then return Queue is full
-
If No, then add the new data element to the location of
tailpointer and increment thetailpointer.
-
-
dequeue: Check if the number of elements in the queue is zero:
-
If Yes, then return Queue is empty
-
If No, then increment the
headpointer.
-
-
size:
-
If,
tail >= head,size = tail - head -
But if,
head > tail, thensize = maxSize - (head - tail)
-
Too much information, give it some time to sink in.
We will be using Python List for implementing the circular queue data structure.
You may also like:



