From 9d9bd35065ded1d78c9a07af2953e69fc8eb61d4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 8 Jan 2023 22:35:13 +0200 Subject: [SDP/HEP] Added preparation for "homework" during class --- .../HomeworkExamPrep/Stack/Stack_Dynamic.hpp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp (limited to 'Year 2/Structures and data algorithms/HomeworkExamPrep/Stack/Stack_Dynamic.hpp') 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 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 +Stack::Stack() : topNode() {} + +template +Stack::~Stack() { + while (topNode != nullptr) + pop(); +} + +template +void Stack::push(const T& v) { + topNode = new Node(v, topNode); +} + +template +void Stack::pop() { + Node* old = topNode; + topNode = topNode->next; + delete old; +} + +template +T& Stack::top() { + return topNode->value; +} + + +#endif -- cgit v1.3.1-23-gd82d