You are on page 1of 4

CIRCULAR QUEUE

#include<iostream.h>
#include<conio.h>
class q
{
int qu[10],front,rear,max,item;
public:
q()
{
max=2;
rear=-1;
front=-1;
}
void ins()
{
cout<<"\nEnter the item to add :";
cin>>item;
if((front==0&&rear==max-1)||(front==rear+1))
cout<<"\nQueue is full";
else if(front==-1)
{
front=0;
rear=0;
qu[rear]=item;
cout<<item<<" is added to queue";
}
else if(rear==max-1)
{
rear=0;
qu[rear]=item;
cout<<item<<" is added to queue";
}
else
{
rear++;
qu[rear]=item;
cout<<item<<" is added to queue";
}
}
void show();
void del()
{
if(front==-1)
{
cout<<"\nQueue is empty";
}
else if(front==rear)
{
item=qu[front];
front=-1;
rear=-1;
cout<<item<<" is deleted";
}
else
{
item=qu[front];
front++;
cout<<item<<" is deleted";
}
}
};
void q::show()
{
int i;
if(front==-1)
cout<<"\nQueue is empty";
else
{
for(i=front;i<=rear;i++)
{
cout<<qu[i]<<" ";
}
cout<<"\nfront="<<front;
cout<<"\nrear="<<rear;
}
}
int main()
{
int choice;
q s;
char ch='y';
clrscr();
cout<<"\n\tmenu\n\n\t1.insert\n\t2.delete\n\t3.show\n\
t4.exit";
do
{
cout<<"\nEnter the choice :";
cin>>choice;
switch(choice)
{
case 1:s.ins();
break;
case 2:s.del();
break;
case 3:s.show();
break;
case 4:return 0;
break;
default: cout<<"\nWrong entry";
break;
}
cout<<"\nDo you want to continue[y/n]:";
cin>>ch;
}
while(ch=='y'||ch=='y');
getch();
return 0;
OUTPUT:

You might also like