diff options
[SDP/HEP] Added preparation for "homework" during class
1 files changed, 53 insertions, 0 deletions
| -rw-r--r-- | Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp | 53 |
diff --git a/Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp b/Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp new file mode 100644 index 0000000..db87f96 --- /dev/null +++ b/Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp @@ -0,0 +1,53 @@ +#ifndef STACK_DYNAMICSEQ +#define STACK_DYNAMICSEQ + +template <class T> +class Stack { + struct Node { + T value; + Node* next; + + Node(const T& v, Node* n) : value(v), next(n) {} + }; + + Node* topNode; + +public: + Stack(); + ~Stack(); + Stack(Stack& other) = delete; + Stack& operator=(const Stack& other) = delete; + + void push(const T& v); + void pop(); + T& top(); +}; + +template <class T> +Stack<T>::Stack() : topNode() {} + +template <class T> +Stack<T>::~Stack() { + while (topNode != nullptr) + pop(); +} + +template <class T> +void Stack<T>::push(const T& v) { + topNode = new Node(v, topNode); +} + +template <class T> +void Stack<T>::pop() { + Node* old = topNode; + topNode = topNode->next; + delete old; +} + +template <class T> +T& Stack<T>::top() { + return topNode->value; +} + + +#endif |
