Circular Queue
/* Program to perform basic operations on Circular Queues */
/* if Queue is EMPTY then empty=1, otherwise empty=0 */
/* if Queue is FULL then empty=0 and ((rear+1)mod n ) = front */
/* initially rear=n-1 and front =0 */
#include<iostream.h>
#include<stdlib.h>
template<class T>
class CQueue
{
int front, rear;
int n,empty;
T *Q;
public:
CQueue();
void enQueue(T);
T deQueue();
void operations();
int isEmpty();
int isFull();
};
template<class T>
CQueue <T>::CQueue()
{
cout<<"\n enter Size of the Queue:";
cin>>n;
Q=new T[n];
front=0;
rear=n-1;
empty=1;
}
template<class T>
void CQueue<T> ::enQueue(T x)
{
rear=(rear+1)%n;
Q[rear]=x;
empty=0;
}
template<class T>
T CQueue<T> ::deQueue()
{
T temp;
temp=Q[front];
front=(front+1)%n;
if((rear+1)%n==front){ empty=1; }
return temp;
}
template<class T>
int CQueue<T>:: isEmpty()
{
return empty==1;
}
template<class T>
int CQueue<T> :: isFull()
{
return (!(empty)&&((rear+1)%n==front));
}
template<class T>
void CQueue<T> :: operations()
{
T x;
while(1)
{
int ch;
cout<<"\n Enter \n 1. for enQueue\n 2. for deQueue\n 3. for EXIT";
cout<<"\n Enter Choice:";
cin>>ch;
switch(ch)
{
case 1: if(isFull()){ cout<<"\n Queue is Full"; break; }
cout<<"\n read data: "; cin>>x; enQueue(x); break;
case 2: if(isEmpty()){ cout<<"\n Queue is Empty"; break; }
cout<<"\n front element of Queue : "<<deQueue();
break;
default: exit(1);
}
}
}
void main()
{
int ch;
cout<<"\n 1. for Integer Queue \n 2. for Character Queue ";
cout<<"\n 3. for float Queue \n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: { CQueue <int> ob1; [Link](); } break;
case 2: { CQueue <char> ob2; [Link](); } break;
case 3: { CQueue <float> ob3; [Link](); } break;
default: cout<<"\n enter right choice"; exit(1);
}
}