Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue{
- public:
- Queue(int size = 1) {
- N_op = 0;
- length = size;
- arr = new int[size];
- begin = 0;
- count = 0;
- end = -1;
- }
- ~Queue() {
- delete []arr;
- }
- void push(int val) {
- if (length == count)
- resize(length * 2);
- ++end %= length;
- arr[end] = val;
- count++;
- }
- void pop(){
- if(!empty()) {
- ++begin %= length;
- count--;
- } else
- cout << "очередь пуста!\n";
- }
- int front(){
- if(!empty())
- return arr[begin];
- else
- cout << "очередь пуста!\n";
- }
- 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;
- }
- bool empty() {
- return size() == 0;
- }
- int size() {
- return length;
- }
- void print() {
- for(int i = begin; i < begin + count; ++i)
- cout << arr[i % length] << ' ';
- cout << endl;
- }
- int N_op;
- private:
- int *arr;
- int length;
- int begin;
- int end;
- int count;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement