Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- namespace lab1{
- class Queue{
- public:
- Queue(int size) : length(size), arr(new int[size]), begin(0), count(0), end(-1){ }
- ~Queue(){ delete []arr;}
- void push(int el){
- if(isFull())
- resize(length + length / 10);
- ++end%=length;
- arr[end] = el;
- count++;
- }
- void pop(){
- if(!isEmpty()){
- arr[begin] = INT_MIN;
- ++begin%=length;
- count--;
- } else {
- throw runtime_error("going beyond the boundaries of the array");
- }
- }
- int front(){
- return arr[begin];
- }
- 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;
- }
- bool isEmpty(){ return size() == 0; }
- bool isFull() { return size() == count; }
- int size() { return length; }
- void sort(){
- for(int i = begin; i < begin + count - 1; ++i){
- int min = i%length;
- for(int j = i + 1; j < begin + count; ++j){
- if(arr[j%length] < arr[min])
- min = j%length;
- }
- swap(arr[i%length], arr[min]);
- }
- }
- void print(){
- for(int i = begin; i < begin + count; ++i)
- cout << arr[i%length] << ' ';
- cout << endl;
- }
- private:
- int *arr;
- int length;
- int begin;
- int end;
- int count;
- };
- }
- #ifndef UNTITLED1_LAB1_H
- #define UNTITLED1_LAB1_H
- #endif //UNTITLED1_LAB1_H
Advertisement
Comments
-
- 12 строка: если length < 10, то размер изменяться не будет. Так же при малом length, ресайз будет неэффективным и 10 <= length < 20, push будет работать всегда за O(n). Замени на length * 2;
- 26 строка: если очередь пуста, то throw;
- 35 строка: утечка памяти, забываешь удалять исходный массив. delete []arr2;
Add Comment
Please, Sign In to add comment
Advertisement