Advertisement
dimon-torchila

Untitled

Jan 2nd, 2023
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. namespace lab1{
  6.     template<typename T>
  7.     class Queue{
  8.     public:
  9.         Queue(int size) : length(size), arr(new T[size]), begin(0), count(0), end(-1){ }
  10.         ~Queue(){ delete []arr; }
  11.         void push(int el){
  12.             if(isFull())
  13.                 resize(length + length / 10);
  14.             ++end%=length;
  15.             arr[end] = el;
  16.             count++;
  17.         }
  18.         void pop(){
  19.             if(!isEmpty()){
  20.                 arr[begin] = INT_MIN;
  21.                 ++begin%=length;
  22.                 count--;
  23.             } else {
  24.                 throw runtime_error("going beyond the boundaries of the array");
  25.             }
  26.         }
  27.         int front(){
  28.             if(!isEmpty())
  29.                 return arr[begin];
  30.             else throw runtime_error("going beyond the boundaries of the array");
  31.         }
  32.         void resize(int new_size){
  33.             int *arr2 = arr;
  34.             arr = new int[new_size];
  35.             for(int i = 0; i < length; ++i){
  36.                 arr[i] = arr2[(i + begin) % length];
  37.             }
  38.             begin = 0;
  39.             end = length - 1;
  40.             length = new_size;
  41.             delete []arr2;
  42.         }
  43.         bool isEmpty(){ return size() == 0; }
  44.         bool isFull() { return size() == count; }
  45.         int size() { return length; }
  46.  
  47.  
  48.         void print(){
  49.             for(int i = begin; i < begin + count; ++i)
  50.                 cout << arr[i%length] << ' ';
  51.             cout << endl;
  52.         }
  53.  
  54.  
  55.  
  56.     private:
  57.         T *arr;
  58.         int length;
  59.         int begin;
  60.         int end;
  61.         int count;
  62.     };
  63. }
  64.  
  65. #ifndef UNTITLED1_LAB1_H
  66. #define UNTITLED1_LAB1_H
  67.  
  68. #endif //UNTITLED1_LAB1_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement