### Queue Operations Using Array in C

/* Queue using Array */
#include<stdio.h>
#define MAXSIZE 4

int cq[MAXSIZE];

int front=-1, rear=-1;

void insert(int item)

{

printf("Insert called:\n");

if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) ||
front==(rear+1)%MAXSIZE )

{

printf("Queue is full.\n");

}

else

{

if(front==-1)

front=rear=0;

else

rear=(rear+1)%MAXSIZE;

cq[rear]=item;

printf("\nfront =%d rear=%d\n",front,rear);

}

}

void display()

{

printf("\n");

int i;

for(i=0;i<MAXSIZE;i++)

{

printf("%d ",cq[i]);

//printf("%d %d ",front,rear);

}

printf("\n");

}

void deletee()

{

if(front==-1)

{

printf("Queue is empty\n");

}

else

{

int temp;

temp=cq[front];

cq[front]=0;

if(front==rear)//only 1 elt was there

front=rear=-1;//delete the single so q is empty

else

front=(front+1)%MAXSIZE;//if more than 1 elt then delete
frm front

printf("deleted elt is: %d\n",temp);

printf("\n front = %d rear = %d\n",front,rear);

}

}

int main()

{

insert(10);

insert(20);

insert(30);

insert(40);

display();

insert(111);//Q is full so make same place by deleting fronts

display();

deletee();

display();

insert(100);

display();

return 0;

}

/*

Insert called:

front =0 rear=0

Insert called:

front =0 rear=1

Insert called:

front =0 rear=2

Insert called:

front =0 rear=3

10 20 30 40

Insert called:

Queue is full.

10 20 30 40

deleted elt is: 10

front = 1 rear = 3

0 20 30 40

Insert called:

front =1 rear=0

100 20 30 40

*/