Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- namespace structs {
- template <class T> class Stack {
- private:
- T *__arr;
- int __top;
- uint __size;
- bool __needEvacuation() { return __top + 2 == __size; }
- void __evacuate() {
- T *newArr = new T[__size * 2];
- std::copy(__arr, __arr + __size, newArr);
- delete[] __arr;
- __arr = std::move(newArr);
- __size *= 2;
- return;
- }
- public:
- Stack(uint size = 2) : __size(size) {
- __top = -1;
- __arr = new T[__size];
- }
- ~Stack() { delete[] __arr; }
- bool IsEmpty() { return __top == -1; }
- void Push(T val) {
- if (__needEvacuation()) {
- __evacuate();
- }
- __arr[++__top] = val;
- }
- T Pop() {
- if (IsEmpty()) {
- std::cout << "Stack is empty" << std::endl;
- return -1;
- }
- return __arr[__top--];
- }
- };
- template <class T> class Queue {
- private:
- T *__arr;
- int __top;
- uint __size;
- bool __needEvacuation() { return __top + 2 == __size; }
- void __evacuate() {
- T *newArr = new T[__size * 2];
- std::copy(__arr, __arr + __size, newArr);
- delete[] __arr;
- __arr = std::move(newArr);
- __size *= 2;
- return;
- }
- public:
- Queue(uint size = 2) : __size(size) {
- __top = -1;
- __arr = new T[__size];
- }
- ~Queue() { delete[] __arr; }
- bool IsEmpty() { return __top == -1; }
- void Push(T val) {
- if (__needEvacuation()) {
- __evacuate();
- }
- __arr[++__top] = val;
- }
- T Pop() {
- if (IsEmpty()) {
- std::cout << "Queue is empty" << std::endl;
- return -1;
- }
- T elem = __arr[0];
- std::copy(__arr + 1, __arr + __size, __arr);
- --__size;
- --__top;
- return elem;
- }
- };
- template <class T> class Deque {
- private:
- T *__arr;
- int __right;
- uint __size;
- bool __needEvacuation() { return __right + 2 == __size; }
- void __evacuate() {
- T *newArr = new T[__size * 2];
- std::copy(__arr, __arr + __size, newArr);
- delete[] __arr;
- __arr = std::move(newArr);
- __size *= 2;
- return;
- }
- public:
- Deque(uint size = 2) : __size(size) {
- __right = -1;
- __arr = new T[size];
- }
- ~Deque() { delete[] __arr; }
- bool IsEmpty() { return __right == -1; }
- void PushLeft(T val) {
- if (__needEvacuation()) {
- __evacuate();
- }
- std::copy(__arr, __arr + __size, __arr + 1);
- __arr[0] = val;
- __right++;
- }
- void PushRight(T val) {
- if (__needEvacuation()) {
- __evacuate();
- }
- __arr[++__right] = val;
- }
- T PopLeft() {
- if (IsEmpty()) {
- std::cout << "Deque is empty" << std::endl;
- return -1;
- }
- T elem = __arr[0];
- std::copy(__arr + 1, __arr + __size, __arr);
- --__size;
- --__right;
- return elem;
- }
- T PopRight() {
- if (IsEmpty()) {
- std::cout << "Deque is empty" << std::endl;
- return -1;
- }
- return __arr[__right--];
- }
- };
- } // namespace structs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement