Circular Queues

The problem with the previous implementation is that the insert function gives a queue-full signal even if a considerable portion is free. This happens because the queue has a tendency to move to the right unless the ‘front’ catches up with the ‘rear’ and both are reset to 0 again (in the delete procedure). To overcome this problem, the elements of the array are required to shift one position left whenever a deletion is made. But this will make the deletion process inefficient. Therefore, an efficient way of overcoming this problem is to consider the array to be circular, as shown in following Figure.

#include <stdio.h>
#define MAX 10 /* The maximum size of the queue */
#include <stdlib.h>

void insert(int queue[], int *rear, int front, int value)
{
   *rear= (*rear +1) % MAX;
   if(*rear == front)
   {
      printf("The queue is full can not insert a value\n");
      exit(0);
   }
   queue[*rear] = value;
}

void delete(int queue[], int *front, int rear, int * value)
{
   if(*front == rear)
   {
      printf("The queue is empty can not delete a value\n");
      exit(0);
   }
   *front = (*front + 1) % MAX;
   *value = queue[*front];
}

int main()
{
   int queue[MAX];
   int front,rear;
   int n,value;
   front=0; rear=0;
   do
   {
      do
      {
            printf("Enter the element to be inserted\n");
         scanf("%d",&value);
         insert(queue,&rear,front,value);
         printf("Enter 1 to continue\n");
         scanf("%d",&n);
      } while(n == 1);

   printf("Enter 1 to delete an element\n");
   scanf("%d",&n);
   while( n == 1)
   {
       delete(queue,&front,rear,&value);
      printf("The value deleted is %d\n",value);
       printf("Enter 1 to delete an element\n");
      scanf("%d",&n);
   }
   printf("Enter 1 to continue\n");
   scanf("%d",&n);
   } while(n == 1);
   getchar();
   return 0;
}