forked from thundergolfer/interview-with-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.hpp
More file actions
68 lines (54 loc) · 1.44 KB
/
stack.hpp
File metadata and controls
68 lines (54 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED
#include <utility> // std::forward
#include <memory> // std::move
#include <stdexcept> // std::runtime_error
class empty_stack : public std::runtime_error {
public:
empty_stack(std::string const& what_arg) : std::runtime_error{what_arg} {};
};
template<typename T>
class Stack {
public:
Stack() : head{nullptr} {};
~Stack();
template<typename U>
void add(U&&);
T remove();
inline bool empty() const { return head == nullptr; };
private:
struct Node {
T data;
Node* next;
template<typename U>
Node(U&& d, Node* n) : data{std::forward<U>(d)}, next{n} {};
};
Node* head;
};
template<typename T>
Stack<T>::~Stack() {
// Rewriting functionality of remove here avoids a call to the move
// constructor for each element in the stack. For code golf, use:
// while(!empty()) remove();
while(!empty()) {
auto old_head = head;
head = head->next;
delete old_head;
}
}
template<typename T>
template<typename U>
void Stack<T>::add(U&& t) {
head = new Node{std::forward<U>(t), head};
}
template<typename T>
T Stack<T>::remove() {
if(empty())
throw empty_stack{"Remove called on empty stack."};
auto old_head = head;
auto old_data = std::move(head->data);
head = head->next;
delete old_head;
return old_data;
}
#endif // STACK_HPP_INCLUDED