Program –
Circular Queue Using Array
#include<iostream.h>
#include<conio.h>
#include<process.h>
int q[5];
const int max=5;
int front=-1,rear=-1;
int item;
void insert();
void dele();
void displayq();
void main()
{
clrscr();
int choice;
do
{
cout<<"\nCircular
Queue Menu\n";
cout<<"1.
Insert\n";
cout<<"2.
Delete\n";
cout<<"3.
Display\n";
cout<<"4.
Exit\n";
cout<<" Enter your
choice: ";
cin>>choice;
switch(choice)
{
case 1: insert();
break;
case 2: dele();
break;
case 3: displayq();
break;
case 4: exit(0);
default:
cout<<"\nPlease enter a valid choice! ";
}
}while(choice!=4);
getch();
}
void insert()
{
if((front==0)&&(rear==max-1)||(front==rear+1))
{
cout<<"\nOverflow!";
getch();
return;
}
cout<<"\nEnter the
item to be inserted: ";
cin>>item;
if(front==-1)
{ front=0;
rear=0;
}
else if(rear==max-1)
rear=0;
else
rear=rear+1;
q[rear]=item;
}
void dele()
{
if(front==-1)
{
cout<<"\nUnderflow!
Queue empty";
getch();
return;
}
cout<<"\nThe deleted
value is "<<q[front]<<"\n";
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==max-1)
front=0;
else
front=front+1;
}
void displayq()
{
if(front==-1)
{
cout<<"\nEmpty
Queue!";
getch();
return;
}
cout<<"\nThe queue
is:\n";
if(front<=rear)
{
for(int i=front;i<=rear;i++)
cout<<q[i]<<"\t";
}
else
{
for(int i=front;i<max;i++)
cout<<q[i]<<"\t";
for(i=0;i<=rear;i++)
cout<<q[i]<<"\t";
}
}