Advertisement
leo11

Queue_test

May 30th, 2021
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. template<typename MyType>
  2. class Queue
  3. {
  4.     MyType *arr;
  5.     int count;
  6.     int _size;
  7.  
  8. public:
  9.  
  10.     Queue(){
  11.         _size = 5;
  12.         arr = new MyType[_size];
  13.         count = 0;
  14.     }
  15.  
  16.     void push(MyType val){
  17.         if (count == _size){
  18.             _size += 5;
  19.             MyType *cpy_arr = new MyType[_size];
  20.             for (int i = 0; i < count; i++)
  21.                 cpy_arr[i] = arr[i];
  22.             delete [] arr;
  23.             arr = cpy_arr;
  24.         }
  25.         arr[count++] = val;
  26.     }
  27.  
  28.     void pop(){
  29.         if (count == 0)
  30.             return;
  31.         for (int i = 0; i < count-1; i++)
  32.             arr[i] = arr[i+1];
  33.         --count;
  34.     }
  35.  
  36.     void pr(){
  37.         if (count == 0){
  38.             std::cout << "No elem in queue" << std::endl;
  39.             return;
  40.         }
  41.         for (int i = 0; i < count; i++)
  42.             std::cout << arr[i] << " ";
  43.         std::cout << std::endl;
  44.     }
  45.  
  46.     MyType front(){
  47.         if (count == 0){
  48.             std::cout << "No elem in queue" << std::endl;
  49.             return 0;
  50.         }
  51.         return arr[0];
  52.     }
  53.  
  54.     bool empty(){
  55.         return count == 0;
  56.     }
  57.  
  58. };
  59.  
  60.  
  61. int main(){
  62.  
  63.     Queue<int> q;
  64.  
  65.     q.push('1');
  66.     q.push('f');
  67.     q.push('g');
  68.     q.push('b');
  69.     q.push('c');
  70.     q.push('v');
  71.     q.pr();
  72.  
  73.     std::cout << q.front() << std::endl;
  74.  
  75.     q.pop();
  76.     q.pop();
  77.     q.pr();
  78.     std::cout << q.front() << std::endl;
  79.     std::cout << q.empty() << std::endl;
  80.  
  81.     q.pop();
  82.     q.pop();
  83.     q.pop();
  84.     q.pop();
  85.     q.pr();
  86.     std::cout << q.empty() << std::endl;
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement