-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.hpp
More file actions
110 lines (88 loc) · 2.27 KB
/
Copy pathstack.hpp
File metadata and controls
110 lines (88 loc) · 2.27 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include "exceptions.hpp"
template <typename T>
class Stack {
T* _data;
int _max_size;
int _cur_size;
public:
Stack();
Stack(const Stack<T>& other);
void push(T elem);
void rewrite_top(T elem);
T pop();
T peek() const;
T penultimate() const; // Предпоследний сверху элемент
inline int size() const { return this->_cur_size; }
inline bool is_empty() const { return this->_cur_size == 0; };
void clear();
~Stack();
};
static const int _DEFAULT_STACK_MAX_SIZE = 10000;
template <typename T>
Stack<T>::Stack() : _max_size(_DEFAULT_STACK_MAX_SIZE), _cur_size(0) {
_data = new T[_max_size];
}
template <typename T>
Stack<T>::Stack(const Stack<T>& other)
: _max_size(other._max_size), _cur_size(other._cur_size) {
_data = new T[_max_size];
for (int i = 0; i < _cur_size; ++i) {
_data[i] = other._data[i];
}
}
template <typename T>
void Stack<T>::push(T elem) {
if (_cur_size >= _max_size) {
throw MicrocircuitException(
"Stack overflow. Unable to push more elements.");
}
_data[_cur_size] = elem;
_cur_size++;
}
template <typename T>
void Stack<T>::rewrite_top(T elem) {
if (_cur_size > 1) {
_data[_cur_size - 1] = elem;
} else {
_data[0] = elem;
}
}
template <typename T>
T Stack<T>::pop() {
if (this->is_empty()) {
throw MicrocircuitException("pop() operation aborted: stack is empty.");
}
T elem = _data[_cur_size - 1];
_cur_size--;
return elem;
}
template <typename T>
T Stack<T>::peek() const {
if (this->is_empty()) {
throw MicrocircuitException(
"peek() operation aborted: stack is empty.");
}
return _data[_cur_size - 1];
}
template <typename T>
T Stack<T>::penultimate() const {
if (this->is_empty()) {
throw MicrocircuitException(
"penultimate() operation aborted: stack is empty.");
} else if (_cur_size < 2) {
throw MicrocircuitException(
"penultimate() operation aborted: stackSize is less than 2.");
}
return _data[_cur_size - 2];
}
template <typename T>
void Stack<T>::clear() {
_cur_size = 0;
}
template <typename T>
Stack<T>::~Stack() {
if (_data) {
delete[] _data;
}
}