aboutsummaryrefslogtreecommitdiff
path: root/src/stack.tpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-01-09 21:47:23 +0100
committerHampusM <hampus@hampusmat.com>2022-01-09 21:47:23 +0100
commit8ceb79db1d0687bba005cef4a77bb889bf7ec3c3 (patch)
treeb7c13359f652506d60c8556ea386ae8d50bfc5bc /src/stack.tpp
parent097aa95c1f0cb159e7d9d0a3edf9284c421ee298 (diff)
refactor: rewrite to c++
Diffstat (limited to 'src/stack.tpp')
-rw-r--r--src/stack.tpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/stack.tpp b/src/stack.tpp
new file mode 100644
index 0000000..958d6ca
--- /dev/null
+++ b/src/stack.tpp
@@ -0,0 +1,36 @@
+#include "stack.hpp"
+#include <iostream>
+#include <stdexcept>
+
+template <typename Item>
+Stack<Item>::Stack(int capacity)
+{
+ _items.reserve(capacity);
+}
+
+template <typename Item>
+void Stack<Item>::push(Item item)
+{
+ if (_items.size() == _items.capacity())
+ throw std::overflow_error("Tried to push when stack is full");
+
+ _items.push_back(item);
+}
+
+template <typename Item>
+void Stack<Item>::pop()
+{
+ if (_items.size() == 0)
+ throw std::underflow_error("Tried to pop when stack size is 0");
+
+ _items.pop_back();
+}
+
+template <typename Item>
+Item Stack<Item>::peek()
+{
+ if (_items.size() == 0)
+ throw std::underflow_error("Tried to peek when stack size is 0");
+
+ return _items.back();
+}