-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.hpp
More file actions
56 lines (47 loc) · 1.06 KB
/
Stack.hpp
File metadata and controls
56 lines (47 loc) · 1.06 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
/*
* Stack.hpp
*
* Created on: 15 nov. 2025
* Author: Boris
*/
#ifndef STACK_HPP
#define STACK_HPP
#include "CppImports.hpp"
/**
* @brief A simple fixed-size stack.
* @tparam T The type of elements stored in the stack.
* @tparam S The maximum number of elements.
*/
template<typename T, size_t S>
class Stack {
public:
Stack() : top_idx(0) {}
bool push(const T& value) {
if (top_idx < S) {
data[top_idx++] = value;
return true;
}
return false;
}
bool pop() {
if (top_idx == 0) {
return false;
}
top_idx--;
return true;
}
// Returns the top element without removing it. Returns default T{} if stack is empty.
T top() const {
if (top_idx == 0) {
return T{};
}
return data[top_idx - 1];
}
size_t size() const { return top_idx; }
size_t capacity() const { return S; }
bool empty() const { return top_idx == 0; }
private:
T data[S];
size_t top_idx;
};
#endif // STACK_HPP