Write c program to perform following functions on circular queue 1) Insert 2) Delete 3) Display

Circular Queue

#include<stdio.h>
#define max 5

//To create structure queue
struct queue
{
int item[max],r,f;
};
typedef struct queue que;

//To define functions
void insertq(que *);
int deleteq(que *);
void display(que);
int isfull(int, int);
int isempty(int);

void main()
{
int ch, data;
   que cq;
   cq.f=cq.r=-1; // f=front ,  r=rear
   do
   {
    printf("\n1.Insert into Queue \n2.Display Q \n3.Delete from Queue \nOther then above to EXIT");
      printf("\nEnter your choice: ");
      scanf("%d",&ch);
      switch(ch)
      {
      case 1:
         insertq(&cq);
         break;
         case 2:
         display(cq);
         break;
         case 3:
         data=deleteq(&cq);
         printf("\n%d is removed from queue",data);
         break;
      }
   }while(ch<=3&&ch>=1);
}

//To insert elements in queue
void insertq(que *cq)
{
   if(isfull(cq->f,cq->r))
   printf("\nQ is full");
   else
   {
    cq->r=(cq->r+1)%max;
      printf("\nEnter the data: ");
      scanf("%d",&cq->item[cq->r]);
      if(cq->r==0&&cq->f==-1)
      cq->f=0;
   }
}

//To check queue is full or not
int isfull(int f, int r)
{
if(f==(r+1)%max)
   return 1;
   else
   return 0;
}
int deleteq(que *cq)
{
int data;
   if(isempty(cq->f))
   printf("\nQ is EMPTY.");
   else
   {
    data=cq->item[cq->f];
      if(cq->f==cq->r)
      cq->f=cq->r=-1;
      else
      cq->f=(cq->f+1)%max;
   }
   return data;
}

//To check queue is empty or not
int isempty(int f)
{
if(f==-1)
   return 1;
   else
   return 0;
}

//To display elements in queue
void display(que cq)
{
int r,f;
   if(isempty(cq.f))
   printf("\nQ is EMPTY.");
   else
   {
    r=cq.r;
      f=cq.f;
      printf("\nContent of Queue is: ");
      while(f!=r)
      {
      printf("%d ",cq.item[f]);
         f=(f+1)%max;
      }
      printf("%d ",cq.item[f]);
   }
}

0 Comments