You are on page 1of 3

IMPLEMENTATION OF CIRCULAR QUEUE USING ARRAY

#include<iostream.h>
#include<conio.h>

const int size=50;

int insert_in_Q(int[],int);
void display(int[],int,int);
int remove(int []);
int Queue[size],front=-1,rear=-1;

void main()
{
int item,res;
char ch=’y’;
clrscr();
while(ch= =’y’||ch= =’Y’)
{
printf(“\n ENTER ITEM FOR INSERTION:”);
scanf (“%d”,&item);
res=insert_in_Q(Queue,item);
if(res= =-1)
{
printf(“OVERFLOW!!!aborting!!\n);
exit(1);
}
printf(“\n NOW THE QUEUE(FRONT ….TO…REAR)IS:\n”);
display(queue,front,rear);
printf(“WANT TO INSERT MORE ELEMENTS(y/n)….”);
scanf(“%c”,&ch);
}
res=insert_in_Q(Queue,item);
if(res= =-1)
{
printf(“UNDERFLOW!!!aborting!!\n);
exit(1);
}
printf(“\n ELEMENT DELETED IS-:%d”,res);
printf(“\n NOW THE QUEUE(FRONT ….TO…REAR)IS:\n”);
display(queue,front,rear);

}
int insert_in_Q(int Queue[],int ele) //FUNCTION TO INSERT AN ELEMENT IN
QUEUE
{
if(rear= =front-1||(front= =0&&rear= =size-1))
return -1;
else if(rear= =-1&&front= =-1)
{
front=0;
rear=0;
Queue[rear]=ele;
}
else
{
rear++;
queue[rear]=ele;
}
return0;
}

void display(intQueue[],intfront,int rear) //FUNCTION TO DISPLAY QUEUE


{
if(front= =-1)
return;
if(rear>=front)
{
for(i=0;i<front;i++)
printf(“\t\t”);
for(int i=front;i<rear;i++)
printf(“%d/t”,Queue[i]);
printf(“%d”,Queue[rear]);

}
else
{
for(i=0;i<rear;i++)
printf(“%d/t”,Queue[i]);
printf(“%d”,Queue[rear]);
for(;i<front;i++)
printf(“\t\t”);
for(i=front;i<size;i++)
printf(“%d/t”,Queue[i]);
}
}

int remove(int queue[]) //FUNCTION TO DELETE ELEMENT


{
int ret;
if(front= =-1)
ret=-1;
else
{
ret=queue[front];

if(front= =rear)
front=rear=-1;
else
{
if(front= =size)
front=0;
else

front++;
}
}
return ret;
}

OUTPUT

ENTER THE ITEM FOR INSERTION:3


NOW THE QUEUE(FRONT ……TO…REAR)IS-:
3
WANT TO INSERT MORE ELEMENTS?(y/n)…y
ENTER THE ITEM FOR INSERTION:3
NOW THE QUEUE(FRONT ……TO…REAR)IS-:
36
WANT TO INSERT MORE ELEMENTS?(y/n)…y
ENTER THE ITEM FOR INSERTION:3
NOW THE QUEUE(FRONT ……TO…REAR)IS-:
368
WANT TO INSERT MORE ELEMENTS?(y/n)…n
ELEMENT DELETED IS -:3
NOW THE QUEUE(FRONT ……TO…REAR)IS-
68