-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
110 lines (91 loc) · 2.09 KB
/
stack.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
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
// Copyright [2019] <PABLO LOPES TEIXEIRA>
#ifndef STRUCTURES_ARRAY_STACK_H
#define STRUCTURES_ARRAY_STACK_H
#include <cstdint> // std::size_t
#include <stdexcept> // C++ exceptions
namespace structures {
template<typename T>
//! CLASSE PILHA
class ArrayStack {
public:
ArrayStack();
explicit ArrayStack(std::size_t max);
~ArrayStack();
void push(const T& data);
T pop();
T& top();
void clear();
std::size_t size();
std::size_t max_size();
bool empty();
bool full();
private:
T* contents;
int top_;
std::size_t max_size_;
static const auto DEFAULT_SIZE = 10u;
};
} // namespace structures
#endif
template<typename T>
structures::ArrayStack<T>::ArrayStack() {
max_size_ = DEFAULT_SIZE;
contents = new T[max_size_];
top_ = -1;
}
template<typename T>
structures::ArrayStack<T>::ArrayStack(std::size_t max) {
max_size_ = max;
contents = new T[max_size_];
top_ = -1;
}
template<typename T>
structures::ArrayStack<T>::~ArrayStack() {
delete [] contents;
}
template<typename T>
void structures::ArrayStack<T>::push(const T& data) {
if (full()) {
throw std::out_of_range("pilha cheia");
} else {
top_++;
contents[top_] = data;
}
}
template<typename T>
T structures::ArrayStack<T>::pop() {
if (empty()) {
throw std::out_of_range("pilha vazia");
} else {
top_--;
return contents[top_+1];
}
}
template<typename T>
T& structures::ArrayStack<T>::top() {
if (empty()) {
throw std::out_of_range("pilha vazia");
} else {
return contents[top_];
}
}
template<typename T>
void structures::ArrayStack<T>::clear() {
top_ = -1;
}
template<typename T>
std::size_t structures::ArrayStack<T>::size() {
return static_cast<std::size_t>(top_)+1;
}
template<typename T>
std::size_t structures::ArrayStack<T>::max_size() {
return max_size_;
}
template<typename T>
bool structures::ArrayStack<T>::empty() {
return top_ == -1;
}
template<typename T>
bool structures::ArrayStack<T>::full() {
return top_ == static_cast<int>(max_size_) - 1;
}