Advertisement
dimon-torchila

Untitled

Jan 2nd, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. namespace lab{
  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.         void countingSort(){
  55.             int mx = INT_MIN;
  56.             for(const auto& it : arr){
  57.                 mx = max(mx, it);
  58.             }
  59.             auto count = vector<int>(mx + 1, 0);
  60.             for(int elem : arr)
  61.                 ++count[elem];
  62.  
  63.         }
  64.  
  65.  
  66.     private:
  67.         T *arr;
  68.         int length;
  69.         int begin;
  70.         int end;
  71.         int count;
  72.     };
  73. }
  74.  
  75. #ifndef UNTITLED1_LAB_H
  76. #define UNTITLED1_LAB_H
  77.  
  78. #endif //UNTITLED1_LAB_H
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement