-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticStack.h
46 lines (33 loc) · 835 Bytes
/
StaticStack.h
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
#pragma once
#include <cstddef>
#include <utility>
/// A Container giving @c LIFO behavior.
template<typename T, std::size_t Size>
class Stack
{
std::size_t top;
T* Array;
public:
Stack() : top(0), Array(new T[Size]) {}
~Stack() { delete[] Array; Array = NULL; }
auto push(const T& data)
{
if (isFull()) return std::pair<bool, T>(false, data);
Array[top++] = data;
return std::pair<bool, T>(true, data);
}
T pop()
{
if (isEmpty()) return T();
return Array[--top];
}
T peek() const
{
if (isEmpty()) return T();
return Array[top - 1];
}
bool isEmpty() const { return (top == 0); }
bool isFull() const { return (top >= Size); }
std::size_t size() const { return Size; }
void clear() { top = 0; }
};