Circular queue using array

 
 /* Program to implement circular queue using array */

 #include<stdio.h>
 void insert(int a[],int n,int val,int *front,int *rear)
 {
  if(*front==0 && *rear==n-1)
   printf("\nQueue overflow... \n");
  else if(*rear == *front - 1)
   printf("\nQueue Overflow...\n");
  else if(*front==-1 && *rear==-1)
  {
   *front=0;
   a[++(*rear)]=val;
  }
  else if(*rear==n-1)
  {
   *rear=0;
   a[*rear]=val;
  }
  else
   a[++(*rear)]=val;
 }
 void del(int a[],int n,int dele,int *front,int *rear)
 {
  if(*front==-1)
  {
   printf("\nQueue underflow ... \n");
   return;
  }
  else if(*front == *rear)
  {
   *front=-1;
   *rear=-1;
  }
  else if(*front==n-1) *front=0;
  else (*front)++;
 }
 void display(int a[],int n,int *front,int *rear)
 {
  int i;
  if(*front<0)
  {
   printf("\nQueue underflow ... \n");
   return;
  }
  else if(*rear >= *front)
  {
   printf("\nQueue :  ");
   for(i=*front;i<=*rear;i++)
    printf("  %d  ",a[i]);
  }
  else 
  {
   printf("\nQueue : ");
   for(i=*front;i<n;i++)
    printf("  %d  ",a[i]);
   for(i=0;i<=*rear;i++)
    printf("  %d  ",a[i]);
  }
  
 }
 main()
 {
  printf("\nProgram to implement circular queue using array : \n");
  int n,val,dele,front=-1,rear=-1,ch;
  printf("\nEnter array capacity : ");
  scanf("%d",&n);
  int a[n];
  do
  {
   printf("\n[1] Insert [2] Delete [3] Display  [4] Quit \n");
   printf("\nEnter your choice : ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
     printf("\nEnter value to insert : ");
     scanf("%d",&val);
     insert(a,n,val,&front,&rear);
     display(a,n,&front,&rear);
     break;

    case 2:
     del(a,n,dele,&front,&rear);
     display(a,n,&front,&rear);
     break;

    case 3 :
     display(a,n,&front,&rear);
     break;
    case 4: break;

    default : printf("\nEnter correct choice....\n");
      break;
   }
  }while(ch!=4);
 }  

Top