Circular Queue Implementation in C++

In linear queue, when we delete any element, only front increment by one but position is not used later. So, when we perform more add and delete operations, memory wastage increases. But in circular queue, memory is utilized if we delete any element that position is used later due to its circular representation.

Algorithm for Circular Queue
1. Declare and initialize necessary variables such as head = 0, tail = 0 etc.
2. For enqueue operation,
If head = (tail+1)%MAXSIZE
print “Queue is Full”
Else
queue[tail] = item
tail = (tail+1)%MAXSIZE
3. For enqueue of next data item, goto step 2
4. For dequeue operation,
If head = tail
print “Queue is Empty”
Else
Remove item i.e. item = queue[head]
Set head = (head + 1)%MAXSIZE
5. For next dequeue operation , goto step 4
6. Stop

Source Code for Circular Queue

SHARE Circular Queue Implementation in C++

You may also like...

5 Responses

  1. Anonymous says:

    The code is wrong…. If the max_size is 4, it works only for 3 values… t

  2. In circular queue there is always 1 blank space. So it works only for 3.

  3. Anonymous says:

    How does that size method work?

  4. Cory says:

    goodness, circular queues are so stupid. Linked lists make so much more sense.

  5. Anonymous says:

    The abs(head == tail) line is suspect

Leave a Reply

Your email address will not be published. Required fields are marked *

Share