Advertisement
Sunjaree

Implementing Queue using array

Mar 3rd, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. #ifndef QUETYPE_H_INCLUDED
  2. #define QUETYPE_H_INCLUDED
  3. template<class T>
  4. class QueType
  5. {
  6. public:
  7.     QueType();
  8.     QueType(int);
  9.     ~QueType();
  10.     void MakeEmpty();
  11.     bool IsEmpty();
  12.     bool IsFull();
  13.     void Enqueue(T);
  14.     void Dequeue(T &);
  15.     T Front();
  16. private:
  17.     int front;
  18.     int rear;
  19.     T *info;
  20.     int maxQue;
  21. };
  22. #endif // QUETYPE_H_INCLUDED
  23.  
  24. *************************************************************
  25. #include "quetype.h"
  26. #include <iostream>
  27. using namespace std;
  28.  
  29. template<class T>
  30. QueType<T>::QueType()
  31. {
  32.     maxQue=501;
  33.     front =  - 1;
  34.     rear =  - 1;
  35.     info=new T[maxQue];
  36. }
  37.  
  38. template<class T>
  39. QueType<T>::QueType(int max)
  40. {
  41.     maxQue= max + 1;
  42.     front = - 1;
  43.     rear =  - 1;
  44.     info=new T[maxQue];
  45. }
  46.  
  47. template<class T>
  48. QueType<T>::~QueType()
  49. {
  50.     delete [] info;
  51. }
  52.  
  53. template<class T>
  54. void QueType<T>:: MakeEmpty()
  55. {
  56.     front=-1;
  57.     rear=-1;
  58.  
  59. }
  60.  
  61. template<class T>
  62. bool QueType<T>::IsEmpty()
  63. {
  64.  
  65. return (rear == front);
  66. }
  67.  
  68. template<class T>
  69. bool QueType<T>::IsFull()
  70. {
  71.     return((rear+1)%maxQue==front);
  72. }
  73.  
  74. template<class T>
  75. void QueType<T>::Enqueue(T item)
  76. {
  77.     if(IsFull())
  78.     {
  79.         cout<<"Queue Overflow "<<endl;
  80.     }
  81.     else
  82.     {
  83.         rear = (rear+1)%maxQue;
  84.         info[rear]=item;
  85.     }
  86. }
  87. template<class T>
  88.  
  89. void QueType<T>::Dequeue(T &item)
  90. {
  91.     if(IsEmpty())
  92.     {
  93.         cout<<"Queue is underflow"<<endl;
  94.     }
  95.     else
  96.     {
  97.         front=(front+1)%maxQue;
  98.         item=info[front];
  99.     }
  100. }
  101. *********************************************************************
  102. #include <iostream>
  103. #include "quetype.cpp"
  104. using namespace std;
  105.  
  106. int main()
  107. {
  108.     QueType <int> ob1(5),ob2;
  109.  
  110.     if(ob1.IsEmpty())
  111.     {
  112.         cout<< "Queue is Empty "<<endl;
  113.     }
  114.     else{
  115.         cout<<"Queue is not Empty "<<endl;
  116.     }
  117.  
  118.     int temp;
  119.     cout << "Enter Four Numbers : ";
  120.  
  121.     for(int i=0; i<4; i++)
  122.     {
  123.         cin>>temp;
  124.         ob1.Enqueue(temp);
  125.     }
  126.  
  127.     if(ob1.IsEmpty())
  128.     {
  129.         cout<< "Queue is Empty"<<endl;
  130.     }
  131.     else
  132.         cout<<"Queue is not Empty"<<endl;
  133.  
  134.     if(ob1.IsFull())
  135.     {
  136.         cout<< "Queue is Full"<<endl;
  137.     }
  138.     else
  139.     {
  140.         cout<<"Queue is not Full"<<endl;
  141.     }
  142.  
  143.  
  144.     cout << "Enter another Integer Number :";
  145.     cin>>temp;
  146.     ob1.Enqueue(temp);
  147.  
  148.     cout<<"Printing value"<<endl;
  149.     for(int i=0; !ob1.IsEmpty(); i++)
  150.     {
  151.         ob1.Dequeue(temp);
  152.         cout << temp << " ";
  153.         ob2.Enqueue(temp);
  154.     }
  155.  
  156.     for(int i=0; !ob2.IsEmpty(); i++)
  157.     {
  158.         ob2.Dequeue(temp);
  159.         ob1.Enqueue(temp);
  160.     }
  161.  
  162.     if(ob1.IsFull())
  163.     {
  164.         cout<< "Queue is Full"<<endl;
  165.     }
  166.     else
  167.     {
  168.         cout<<"Queue is not Full"<<endl;
  169.     }
  170.  
  171.     ob1.Dequeue(temp);
  172.     ob1.Dequeue(temp);
  173.  
  174.     cout<<"Printing value"<<endl;
  175.     for(int i=0; !ob1.IsEmpty(); i++)
  176.     {
  177.         ob1.Dequeue(temp);
  178.         cout << temp << " ";
  179.         ob2.Enqueue(temp);
  180.  
  181.     }
  182.  
  183.     for(int i=0; !ob2.IsEmpty(); i++)
  184.     {
  185.         ob2.Dequeue(temp);
  186.         ob1.Enqueue(temp);
  187.     }
  188.  
  189.  
  190.     ob1.Dequeue(temp);
  191.     ob1.Dequeue(temp);
  192.     ob1.Dequeue(temp);
  193.  
  194.     if(ob1.IsEmpty())
  195.     {
  196.         cout<< "Queue is Empty"<<endl;
  197.     }
  198.     else
  199.         cout<<"Queue is not Empty"<<endl;
  200.  
  201.     cout << "Enter Five Numbers : ";
  202.  
  203.     for(int i=0; i<5; i++)
  204.     {
  205.         cin>>temp;
  206.         ob1.Enqueue(temp);
  207.     }
  208.  
  209.     cout<<"Reverse printing value"<<endl;
  210.     int array[5];
  211.     for(int i=0; !ob1.IsEmpty(); i++)
  212.     {
  213.         ob1.Dequeue(temp);
  214.         array[i]=temp;
  215.     }
  216.     for(int i=4; i>=0; i--)
  217.     {
  218.         cout<< array[i]<< " ";
  219.  
  220.     }
  221.  
  222.     cout << endl;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement