diff --git a/include/stack.h b/include/stack.h index 51b7a05..c1d49e2 100644 --- a/include/stack.h +++ b/include/stack.h @@ -3,6 +3,7 @@ #include #include +#include #define DEFAULT_STACK_SIZE 4 @@ -46,6 +47,31 @@ Stack::~Stack() { delete[] this->data; } +template +StackErr Stack::push(T value) { + // If not enough space allocate more space + if (this->len >= this->cap) { + uint64_t new_capacity = this->cap * 2; + T *tmp; + try { + tmp = new T[new_capacity]; + } catch (const std::bad_alloc& e) { + return StackErr::bad_alloc; + } + + for (int i = 0; i < this->len; i++) { + tmp[i] = this->data[i]; + } + + delete[] this->data; + this->data = tmp; + } + + this->data[this->len] = value; + this->len++; + + return StackErr::ok; +} #endif // !ST