Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
stack.hpp
Go to the documentation of this file.
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 */
18template <class ValueType>
19struct node {
20 ValueType data = {}; ///< data at current node
22 {}; ///< pointer to the next ::node instance
23};
24
25template <typename Node, typename Action>
26void 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 */
37template <class ValueType>
38class 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 });
49 std::cout << "Size of stack: " << size << std::endl;
50 }
51
52 std::vector<value_type> toVector() const {
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:
102 {}; /**< Pointer to the stack */
103 std::size_t size = 0; ///< size of stack
104};
105
106#endif // DATA_STRUCTURES_STACK_HPP_
Definition: stack.hpp:38
std::size_t size
size of stack
Definition: stack.hpp:103
bool isEmptyStack() const
Definition: stack.hpp:70
void pop()
Definition: stack.hpp:88
void clear()
Definition: stack.hpp:95
void display() const
Definition: stack.hpp:43
void push(const value_type &item)
Definition: stack.hpp:73
std::shared_ptr< node< value_type > > stackTop
Definition: stack.hpp:101
value_type top() const
Definition: stack.hpp:82
T endl(T... args)
T get(T... args)
T reserve(T... args)
Definition: linkedlist_implentation_usingarray.cpp:14
for std::vector
Definition: avltree.cpp:13