Friday 20 January 2017

Array Implementation of circular queue using modulous operator : C Program

#include <stdio.h>

#include <stdlib.h>

#define MAX 5

int queue[MAX],f=-1,r=-1;

void enq();

void deq();

void display();

main()

{

 int n,i;

 printf("enter the number of elements");

 scanf("%d",&n);

 for(i=1;i<=n;i++)

  enq();

 display();

 printf("\nAfter one deletion queue elements are\n");

 deq();

 display();

}

void enq()
{

 int val;

 if(f == (r+1)%MAX)
 {
     printf("Queue full");
 }
 else 
 {
     if(f == -1 && r == -1)
        f=r=0;
     else
        r=(r+1)%MAX;

     printf("enter the data to be entered\n");

     scanf("%d",&val);

     queue[r]=val;
 }
}

void deq()
{

 if(f==-1)
       printf(" Queue is empty or underflow");
 else
 {
      printf("deleted element = %d",queue[f]);

      if(f ==r)

          f=r=-1;

      else

         f=(f+1)%MAX;
  }
}

void display()
{

int i;
if(f<=r)
{
   for(i=f; i<=r;i++)
          printf("%d\t",queue[i]);
}
else
{
   for(i=f;i<MAX;i++)
           printf("%d\t",queue[i]);
   for(i=0;i<=r;i++)
           printf("%d\t",queue[i]);
}
}



Output:


No comments:

Post a Comment