0% found this document useful (0 votes)
96 views

Circular Queue

This document defines a circular queue data structure and provides functions to perform basic queue operations. A circular queue is implemented using a fixed-size array with front and rear pointers. Functions are provided to enqueue an element, dequeue an element, check if the queue is empty or full, and perform sample operations on the queue. The main function allows the user to choose between integer, character, or float queues and calls the operations function to demonstrate usage.

Uploaded by

saisravan0123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Circular Queue

This document defines a circular queue data structure and provides functions to perform basic queue operations. A circular queue is implemented using a fixed-size array with front and rear pointers. Functions are provided to enqueue an element, dequeue an element, check if the queue is empty or full, and perform sample operations on the queue. The main function allows the user to choose between integer, character, or float queues and calls the operations function to demonstrate usage.

Uploaded by

saisravan0123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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; ob1.operations(); } break;
 
                   case 2:  { CQueue <char> ob2; ob2.operations(); } break;
 
                   case 3:  { CQueue <float> ob3; ob3.operations(); } break;
 
                   default: cout<<"\n enter right choice"; exit(1);
     }
 
 
}
 
 

You might also like