#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define Max 5
class circularqueue
{
private:
int cq[Max],front,rear;
public:
circularqueue()
{
front=rear=-1;
cout<<"Circular Queue is Created"<<endl;
}
void menu();
void insert();
void del();
void display();
};
void circularqueue::menu()
{
int ch;
do
{
cout<<endl;
cout<<"CIRCULAR QUEUE MENU"<<endl
<<"1.Insert"<<endl
<<"2.Delete"<<endl
<<"3.Display"<<endl
<<"4.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:
cout<<"Sorry! invalid choice"<<endl;
}
}while(ch!=4);
}
void circularqueue::insert()
{
int temp;
if((front==0 && rear==Max-1) || (front==rear+1))
{
cout<<"Queue is overflow"<<endl;
return;
}
if(front==-1)
{
front=rear=0;
}
else
if(rear==Max-1)
rear=0;
else
rear=rear+1;
cout<<"Enter an element to Queue : ";
cin>>temp;
cq[rear]=temp;
}
void circularqueue::del()
{
if(front==-1)
{
cout<<"Circular Queue is underflow"<<endl;
return;
}
cout<<"Elements deleted from cirqueue is : "<<cq[front];
if(front==rear)
{
front=rear=-1;
}
else
if(front==Max-1)
front=0;
else
front=front+1;
}
void circularqueue::display()
{
if(front==-1)
{
cout<<"Circular Queue is empty "<<endl;
return;
}
cout<<"Front : ";
if(front<=rear)
for(int i=front;i<=rear;i++)
cout<<cq[i]<<" ";
else
{
for(int i=front;i<=Max-1;i++)
cout<<cq[i]<<" ";
for(i=0;i<=rear;i++)
cout<<cq[i]<<" ";
}
cout<<" : Rear";
}
void main()
{
clrscr();
circularqueue cq;
cq.menu();
getch();
}
No comments:
Post a Comment