Program in C++ to implement queue Using Template feature.
#include<iostream> #include<conio.h> using namespace std; template <class T> class Queue { T *q; int front,rear,size; public: Queue() { front=rear=-1; size=35; q=new T[size]; for(int i=0;i<size;i++) q[i]=0; } Queue(int s) { front=rear=-1; size=s; q=new T[size]; for(int i=0;i<size;i++) q[i]=0; } ~Queue(){delete q;} int isEmpty() { return front==-1||front>rear; } int isFull() { return rear==size-1; } void insert(T n) { if(isFull()){ cout<<"\nQueue is full!"; return; } if(front==-1) front=0; rear+=1; q[rear]=n; } void del() { if(isEmpty()){ cout<<"\nQueue is empty!"; return; } cout<<"\nDeleted data is "<<q[front]; front++; } void showRev() { int i; if(isEmpty()){ cout<<"\nQueue is empty!"; return; } printf("\nEntered stack elements is "); for(i=front;i<=rear;i++) cout<<q[i]<<" "; } void show() { int i; if(isEmpty()){ cout<<"\nQueue is empty!"; return; } printf("\nEntered stack elements is "); for(i=rear;i>=front;i--) cout<<q[i]<<" "; } int length() { return (rear-front+1); } }; int main() { Queue<int> q1; q1.insert(56); q1.insert(34); q1.insert(45); q1.insert(16); q1.insert(54); q1.insert(95); q1.show(); Queue<char> q2; q2.insert('e'); q2.insert('r'); q2.insert('o'); q2.insert('p'); q2.insert('k'); q2.insert('f'); q2.show(); cout<<"\nsize of queue = "<<q2.length(); q2.del(); q2.del(); q2.show(); Queue<float> q3(2); q3.insert(23.8); q3.insert(78.8); q3.insert(45.5); q3.show(); cout<<"\nsize of queue = "<<q3.length(); return 0; }
Output :-
0 Comments