Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- namespace lab1{
- template<typename T>
- class Queue{
- public:
- unsigned long long N_op = 0;
- Queue(int size) : length(size), arr(new T[size]), begin(0), count(0), end(-1){ N_op+=7; }
- ~Queue(){ delete []arr; N_op++;}
- void push(int el){
- if(isFull())
- resize(length + length / 10);
- ++end%=length;
- arr[end] = el;
- count++;
- N_op+=8;
- }
- void pop(){
- if(!isEmpty()){
- arr[begin] = INT_MIN;
- ++begin%=length;
- count--;
- } else {
- throw runtime_error("going beyond the boundaries of the array");
- }
- N_op+=8;
- }
- int front(){
- if(!isEmpty())
- return arr[begin];
- else throw runtime_error("going beyond the boundaries of the array");
- N_op+=5;
- }
- void resize(int new_size){
- int *arr2 = arr;
- arr = new int[new_size];
- for(int i = 0; i < length; ++i){
- arr[i] = arr2[(i + begin) % length];
- }
- begin = 0;
- end = length - 1;
- length = new_size;
- delete []arr2;
- N_op+=length + 13;
- }
- bool isEmpty(){ return size() == 0; N_op+=2; }
- bool isFull() { return size() == count; N_op+=2; }
- int size() { return length; N_op++; }
- void print(){
- for(int i = begin; i < begin + count; ++i)
- cout << arr[i%length] << ' ';
- cout << endl;
- }
- private:
- T *arr;
- int length;
- int begin;
- int end;
- int count;
- };
- }
- #ifndef UNTITLED1_LAB1_H
- #define UNTITLED1_LAB1_H
- #endif //UNTITLED1_LAB1_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement