Circular Queue implementation in C++ program using template functions: This CPP tutorial code is totally based on Data structures operation. This queue implementation can be done using template functions, Arrays and switch case mechanism between classes. Few operations are performing by this source code. Those implementations are,
- Insertion of Queue Element,
- Deletion of Queue values,
- Display the Circular Queue.
Recommended Reading:
Circular queue in C++ using Template:
Good Read: Learn English Quickly
// Header Files section #include<iostream.h> #include<conio.h> #include<stdlib.h> template<class T> // Template declaration class CircularQ { T k[22]; int rear,front,N; public: CircularQ() // Constructor Function { cout<<"\n What is your Circular Queue Size:?\n"; cin>>N; rear=front=N-1; } void Add(T elt) { if((rear+1)%n!=front) { rear=(rear+1)%N; k[rear]=elt; } else cout<<"\n Currently the queue is Full. Unable to Add more"<<elt<<endl; } void Delete() { if(front==rear) cout<<"\n Currently the queue is Empty.\n"; else { front=(front+1)%N; cout<<"\n Removed element : "<<k[front]; } } void CircularQ_oprn(); void Output_Display(); }; template<class T> void CircularQ<T> :: Output_Display() { if(rear!=front) { cout<<"\n Circular Queue Elements:\n"; for(int i=(front+1)%n;;i=(i+1)%N) { cout<<k[i]<<"\t"; if(i==rear) break; } } else cout<<"\n There is no Queue elements to display \n"; } template<class T> void Queue<T> :: CircularQ_oprn() { int select=1,i; T elt; while(select>0 && select<3) { cout<<"\n 1. For Adding Queue Value \n 2.For Deleting Queue Value \n Press any key to Exit\n Enter your Choice Here:"; cin>>select; switch(select) { case 1 : // For Adding cout<<" Enter the Element for Adding:\n"; cin>>elt; Add(elt); Output_Display(); break; case 2 : // For Deletion Delete(); Output_Display(); break; default : exit(0); } } } void main() { clrscr(); cout<<"\n\t CircularQueue implementation in C++ with Arrays \n"; cout<<" Circular Queue Integer Value\n"; CircularQ<int> Q1; cout<<"Circular Queue Float Value\n"; CircularQ<float> Q2; int cha; while(1) { cout<<" Circular Queue implementation \n\n"; cout<<" 1. For Integer Queue \n 2. For Float Queue \n Press any key to exit\n\n Enter your Choice Here:"; cin>>cha; switch(cha) { case 1 : // It performs the CircularQueue operation on Integer Q1.CircularQ_oprn(); break; case 2 : // It performs the CircularQueue operation on Float Q2.CircularQ_oprn(); break; default : exit(0); } } }