Javatpoint Logo
Javatpoint Logo

Circular Queue

Deletions and insertions can only be performed at front and rear end respectively, as far as linear queue is concerned.

Consider the queue shown in the following figure.


Circular Queue

The Queue shown in above figure is completely filled and there can't be inserted any more element due to the condition rear == max - 1 becomes true.

However, if we delete 2 elements at the front end of the queue, we still can not insert any element since the condition rear = max -1 still holds.

This is the main problem with the linear queue, although we have space available in the array, but we can not insert any more element in the queue. This is simply the memory wastage and we need to overcome this problem.


Circular Queue

One of the solution of this problem is circular queue. In the circular queue, the first index comes right after the last index. You can think of a circular queue as shown in the following figure.


Circular Queue

Circular queue will be full when front = -1 and rear = max-1. Implementation of circular queue is similar to that of a linear queue. Only the logic part that is implemented in the case of insertion and deletion is different from that in a linear queue.

Complexity

Time Complexity

Front O(1)
Rear O(1)
enQueue() O(1)
deQueue() O(1)

Insertion in Circular queue

There are three scenario of inserting an element in a queue.

  1. If (rear + 1)%maxsize = front, the queue is full. In that case, overflow occurs and therefore, insertion can not be performed in the queue.
  2. If rear != max - 1, then rear will be incremented to the mod(maxsize) and the new value will be inserted at the rear end of the queue.
  3. If front != 0 and rear = max - 1, then it means that queue is not full therefore, set the value of rear to 0 and insert the new element there.

Algorithm to insert an element in circular queue

  • Step 1: IF (REAR+1)%MAX = FRONT
    Write " OVERFLOW "
    Goto step 4
    [End OF IF]
  • Step 2: IF FRONT = -1 and REAR = -1
    SET FRONT = REAR = 0
    ELSE IF REAR = MAX - 1 and FRONT ! = 0
    SET REAR = 0
    ELSE
    SET REAR = (REAR + 1) % MAX
    [END OF IF]
  • Step 3: SET QUEUE[REAR] = VAL
  • Step 4: EXIT

C Function

Algorithm to delete an element from a circular queue

To delete an element from the circular queue, we must check for the three following conditions.

  1. If front = -1, then there are no elements in the queue and therefore this will be the case of an underflow condition.
  2. If there is only one element in the queue, in this case, the condition rear = front holds and therefore, both are set to -1 and the queue is deleted completely.
  3. If front = max -1 then, the value is deleted from the front end the value of front is set to 0.
  4. Otherwise, the value of front is incremented by 1 and then delete the element at the front end.

Algorithm

  • Step 1: IF FRONT = -1
    Write " UNDERFLOW "
    Goto Step 4
    [END of IF]
  • Step 2: SET VAL = QUEUE[FRONT]
  • Step 3: IF FRONT = REAR
    SET FRONT = REAR = -1
    ELSE
    IF FRONT = MAX -1
    SET FRONT = 0
    ELSE
    SET FRONT = FRONT + 1
    [END of IF]
    [END OF IF]
  • Step 4: EXIT

C Function

Menu-Driven program implementing all the operations on a circular queue

Output:

**********Main Menu**********

=============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element
1

Value inserted 
**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element
2

Value inserted 
**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element
3

Value inserted 
**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

Circular Queue Elements are : 
1
2
3

**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2

**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element
4

Value inserted 
**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

Circular Queue Elements are : 
2
3
4

**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element
1

OVERFLOW
**********Main Menu**********

=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?

4

Next TopicDS Tree




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA