In this article circular queue in c we give the information about we also call circular queue as ring-buffer. The last node in the circular queue is connected to the first node. So that the circle is formed.
Circular queue in C:-
We also call circular queue as ring-buffer. The last node in the circular queue is connected to the first node. So that the circle is formed. It works on the principle of FIFO. In Circular Queue, item is added from rear end and item is removed from front end.
A Circular queue overcomes the problem of unused space in linear queues. A circular queue also has front and rear to keep the track of the elements to be deleted and inserted. The following assumptions are made about circular queue:-
- Front always points the first elements in circular queue.
- Rear is incremented every time when a new element is to added.
- Front is incremented every time when an element is to deleted.
- If front = rear then queue is empty.
Operations on Circular queue:-
The following figures shows the add and delete operations on queue.
There are two operations in circular queue
- Add operation on queue
- Delete operation on queue.
1.Add Operation in Circular Queue:-
First check whether queue is full or not and then add an elements in queue.
If front =0 and rear =MAX-1 or front =rear +1 then queue is full
if((front==0 && rear == MAX-1) || (front == rear +1))
printf(“\n Queue is overflow…”);
If queue is initially empty then set the value of front and rear to -1 and then add an element in queue. Otherwise increase the value of rear only and then add an element.
If queue is full and rear = MAX-1 then set the value of rear to 0 and add an element.
front = 0;
rear = 0;
if(rear == MAX – 1)
rear =0 ;
rear =rear +1;
2.Delete operation on queue:-
First check whether queue is empty or not and then delete an element from queue.
If front = -1 and rear =-1 then queue is said to be empty.
If front = MAX – 1 then delete an elements from queue and set the value of front = 0.
if(front == MAX-1)
if there is only one element in queue then delete an element from queue and set the value of front and rear to -1.
if (front ==rear)
front = -1;
rear = -1;
Circular Queue Example:-
/* Implementation of Circular Queue
This program implements the insert and delete operation in circular queue */
#define MAX 5
int n,ch; // variable declaration
void insert(int); // function declaration
printf(“\n1:Insert \n2:Delete \n3:Display \n4:Exit”);
printf(“\n Enter your Choice: “);
printf(“\n Enter number to insert: “);
printf(“\n Enter correct choice…”);
void insert(int no)
if((q.front==0 && q.rear==MAX-1) || (q.front==q.rear+1))
printf(“\n Queue Overflow…”);
printf(“\n %d is inserted into QUEUE…”,no);
printf(“\n Queue is Empty…”);
printf(“\n %d is deleted…”,temp);
printf(“\n Queue is Empty”);