Menu Close

Circular Queue in C

Circular Queue:

  • Linear queue follows First In First Out rule.
  • We insert elements from Rear and delete from Front.
  • In linear queue “Front” is fixed location.
  • When we delete an element from the queue, shifting of elements takes much time.
  • In circular queue, we are representing the Queue in circle form.
  • We can move front location also after deletion of element from the queue.
  • As front value is not fixed, front and rear values starts with -1

Declaration:

  • We declare array variable with fixed size.
  • Front and Rear variables are used to process the location data.
  • Front and Rear variables not pointing to any location initially.
  • Front location is not fixed in Circular Queue.
    • int  cqueue[8];
    • int front=-1;
    • rear=-1;

Operations: We can perform following operations on Circular Queue.

  1. Insertion
  2. Deletion
  3. Display

Insertion:

  • The following diagrams describe the queue initially.
  • Inserting elements from ‘rear’.
  • First insertion changes both the values of front and rear variables.
  • Continues insertion of elements results “Queue is full”.
  • Deleting elements using ‘front’ variable.
  • After deletion of element, front value increase by 1.

When we try to insert the element and the rear value reaches size-1, again rear value starts from 0.

  • Keep on inserting elements reaches “Queue is full” condition.
  • Two conditions gives “Queue is full” situation.

While deleting, if it reaches the location “size-1”, it starts with 0 again in the next cycle.

  • When front and rear values are equal means Queue has only 1 element.
  • When we remove the last element from the queue, then front and read values become -1.