Advertisement
dimon-torchila

Untitled

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