|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author danghai |
| 4 | + * @author [Piotr Idzik](https://github.com/vil02) |
| 5 | + * @brief This class specifies the basic operation on a stack as a linked list |
| 6 | + **/ |
| 7 | +#ifndef DATA_STRUCTURES_STACK_HPP_ |
| 8 | +#define DATA_STRUCTURES_STACK_HPP_ |
| 9 | + |
| 10 | +#include <iostream> /// for IO operations |
| 11 | +#include <memory> /// for std::shared_ptr |
| 12 | +#include <stdexcept> /// for std::invalid_argument |
| 13 | +#include <vector> /// for std::vector |
| 14 | + |
| 15 | +/** Definition of the node as a linked-list |
| 16 | + * \tparam ValueType type of data nodes of the linked list should contain |
| 17 | + */ |
| 18 | +template <class ValueType> |
| 19 | +struct node { |
| 20 | + ValueType data = {}; ///< data at current node |
| 21 | + std::shared_ptr<node<ValueType>> next = |
| 22 | + {}; ///< pointer to the next ::node instance |
| 23 | +}; |
| 24 | + |
| 25 | +template <typename Node, typename Action> |
| 26 | +void traverse(const Node* const inNode, const Action& action) { |
| 27 | + if (inNode) { |
| 28 | + action(*inNode); |
| 29 | + traverse(inNode->next.get(), action); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** Definition of the stack class |
| 34 | + * \tparam value_type type of data nodes of the linked list in the stack should |
| 35 | + * contain |
| 36 | + */ |
| 37 | +template <class ValueType> |
| 38 | +class stack { |
| 39 | + public: |
| 40 | + using value_type = ValueType; |
| 41 | + |
| 42 | + /** Show stack */ |
| 43 | + void display() const { |
| 44 | + std::cout << "Top --> "; |
| 45 | + traverse(stackTop.get(), [](const node<value_type>& inNode) { |
| 46 | + std::cout << inNode.data << " "; |
| 47 | + }); |
| 48 | + std::cout << std::endl; |
| 49 | + std::cout << "Size of stack: " << size << std::endl; |
| 50 | + } |
| 51 | + |
| 52 | + std::vector<value_type> toVector() const { |
| 53 | + std::vector<value_type> res; |
| 54 | + res.reserve(this->size); |
| 55 | + traverse(stackTop.get(), [&res](const node<value_type>& inNode) { |
| 56 | + res.push_back(inNode.data); |
| 57 | + }); |
| 58 | + return res; |
| 59 | + } |
| 60 | + |
| 61 | + private: |
| 62 | + void ensureNotEmpty() const { |
| 63 | + if (isEmptyStack()) { |
| 64 | + throw std::invalid_argument("Stack is empty."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public: |
| 69 | + /** Determine whether the stack is empty */ |
| 70 | + bool isEmptyStack() const { return (stackTop == nullptr); } |
| 71 | + |
| 72 | + /** Add new item to the stack */ |
| 73 | + void push(const value_type& item) { |
| 74 | + auto newNode = std::make_shared<node<value_type>>(); |
| 75 | + newNode->data = item; |
| 76 | + newNode->next = stackTop; |
| 77 | + stackTop = newNode; |
| 78 | + size++; |
| 79 | + } |
| 80 | + |
| 81 | + /** Return the top element of the stack */ |
| 82 | + value_type top() const { |
| 83 | + ensureNotEmpty(); |
| 84 | + return stackTop->data; |
| 85 | + } |
| 86 | + |
| 87 | + /** Remove the top element of the stack */ |
| 88 | + void pop() { |
| 89 | + ensureNotEmpty(); |
| 90 | + stackTop = stackTop->next; |
| 91 | + size--; |
| 92 | + } |
| 93 | + |
| 94 | + /** Clear stack */ |
| 95 | + void clear() { |
| 96 | + stackTop = nullptr; |
| 97 | + size = 0; |
| 98 | + } |
| 99 | + |
| 100 | + private: |
| 101 | + std::shared_ptr<node<value_type>> stackTop = |
| 102 | + {}; /**< Pointer to the stack */ |
| 103 | + std::size_t size = 0; ///< size of stack |
| 104 | +}; |
| 105 | + |
| 106 | +#endif // DATA_STRUCTURES_STACK_HPP_ |
0 commit comments