Program to Implement Circular Queue using Array in c++

/*program to implement circular queue using array in c++ */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 5
class queue
{
int q[MAX];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}

void display();
void insert(int x);
int delete_q();
};
void queue:: display()
{
int i;
int fpos,rpos;
fpos=front;
rpos=rear;
if(fpos<=rpos)
{
while(fpos<=rpos)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
}
else
{
while(fpos<=MAX-1)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
fpos=0;
while(fpos<=rpos)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
}
}
void queue:: insert(int x)
{
if((front==0 && rear==MAX-1)||(front>rear))
{
cout<<“\n queue full “;
return;
}
else
{
if(rear==MAX-1)// && front >0)
{
rear=0;
}
else
rear=rear+1;
if(front==-1)
{
front=0;
}
q[rear]=x;
}
}
int queue:: delete_q()
{
int ele;
if(front==-1)
{
cout<<“\n queue empty “;
ele=0;
return ele;
}
ele=q[front];
if(front==rear)
{
front=rear=-1;
}
else
{
if(front==MAX-1)
front=0;
else
front=front+1;
}
return ele;

}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete \n3.Display n4.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_q();
cout<<“\n ele= “<<ele;
break;
case 3:
obj.display();
break;

}
}while(ch!=4);
}

Leave a Reply