// QUEUE
// Program 6 : To implement circular QUEUE using array

#include <stdio.h>
#include <conio.h>

#define MAX 20

int cqueue[MAX];
int front = -1;
int rear  = -1;

 int insert(int );
 int delete();
void display();

main()
{
  int x, opn;

  do
  {
    printf("\n\n\t <1>INSERT    <2>DELETE    <3>DISPLAY    <4>EXIT");
    printf("\n\t Enter your option : ");
    scanf("%d",&opn);

    switch(opn)
    {
		case 1:
			printf("\t Enter number to insert : ");
	      	scanf("%d",&x);
	      	if(insert(x))
	       		printf("\t ERROR : OVERFLOW !!!");
	      	printf("\t Press any key to continue ...");
	      	getch();
	      	break;

      	case 2:
			if( front == -1)
	       		printf("\t ERROR : UNDERFLOW !!!");
	       	else
	       		printf("\t deleted element is : %d", delete());
	      	printf("\n\t Press any key to continue ...");
	      	getch();
	      	break;

      	case 3:
			if( front == -1 )
	       		printf("\t Circular QUEUE IS EMPTY");
	      	else
	       		display();
	      	printf("\n\t Press any key to continue ...");
	      	getch();
	      	break;
    }
  }while(opn!=4);
}
// end of main

int insert(int x)
{
  	if((front == 0 && rear == MAX-1) || front == rear+1 )
   		return -1; // overflow
   	
  	if(front == -1)
   		front = rear = 0;
  	else
   		rear = (rear+1)%MAX;
  	
	cqueue[rear] = x;
  	return 0;
}

int delete()
{
  	int val;
  	
	if( front == -1 )
   		return -1;
  	else
		val = cqueue[front];
  	
	if( front == rear ) //only one element
   		front = rear = -1;
  	else
   		front = (front+1)%MAX;
  	
	return val;
}

void display()
{
  	int i;

  	if( front == -1 || front>rear )
   		return ;

  	printf("\n\t Cir. QUEUE = [ ");
  	for( i=front; i<=rear; i++)
   		printf(" %d ",cqueue[i]);
  	printf("]");
}



Output