Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STRUCTURES_ARRAY_STACK_H
- #define STRUCTURES_ARRAY_STACK_H
- #include <cstdint> // std::size_t
- #include <stdexcept> // C++ exceptions
- namespace structures {
- template<typename T>
- /*
- * class stack
- */
- class ArrayStack {
- private:
- /*
- * atributes of class stack
- */
- T* contents;
- int top_;
- std::size_t max_size_;
- const static auto DEFAULT_SIZE = 10u;
- public:
- /*
- * standard constructor
- */
- ArrayStack() {
- max_size_ = DEFAULT_SIZE;
- contents = new T[max_size_];
- top_ = -1;
- }
- /*
- * constructor with size of array as parameter
- */
- explicit ArrayStack(std::size_t max) {
- contents = new T[max];
- max_size_ = max;
- top_ = -1;
- }
- /*
- * destructor
- */
- ~ArrayStack() {
- delete contents;
- }
- /*
- * inserts data on top_ of the stack
- */
- void push(const T& data) {
- if (full())
- throw 1;
- contents[++top_] = data;
- }
- /*
- * remove data from the stack
- */
- T pop() {
- if (empty())
- throw 2;
- return contents[top_--];
- }
- /*
- * points to the top_ of the stack
- */
- T& top() {
- return contents[top_];
- }
- /*
- * points to the beggining of the stack
- */
- void clear() {
- top_ = -1;
- }
- /*
- * returns actual size of the stack
- */
- std::size_t size() {
- return top_ + 1;
- }
- /*
- * returns max_size_ of the stack
- */
- std::size_t max_size() {
- return max_size_;
- }
- /*
- * verifies if stack is empty
- */
- bool empty() {
- if (top_ == -1)
- return true;
- return false;
- }
- /*
- * verifies if stack is full
- */
- bool full() {
- if (top_ == (max_size_-1))
- return true;
- return false;
- }
- };
- } // namespace structures
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement