Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / HomeworkExamPrep / Stack / Stack_Dynamic.hpp
blob: db87f961557c7d096b1bdc79e3dc93bb3ccec94d (plain) (blame)
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
#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