-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.h
83 lines (60 loc) · 1.82 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
#ifndef _MY_STACK_H_
#define _MY_STACK_H_
#include <vector>
namespace amo {
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
template<class T>
class Stack{
private:
std::vector<T> vector;
public:
Stack() {
std::cout << "[Stack::Stack()]: this:" << this << WHITE << std::endl;
}
~Stack() {
std::cout << "[Stack::~Stack()]: this:" << this << WHITE << std::endl;
}
typedef struct functor_traverse {
void operator() (T& t) {
std::cout << "[Stack::VST::operator()(T&)]: element:" << t << " at " << &t << WHITE << std::endl;
}
} VST;
void push(T const& t);
T pop();
T& top();
void traverse();
bool empty() { return vector.size() == 0 ? true : false;}
};
template<class T>
void amo::Stack<T>::traverse() {
VST functor;
std::cout << "[Stack::pop()]: --- STACK TOP ------" << WHITE << std::endl;
for (typename std::vector<T>::iterator it=std::next(vector.end(),-1); it!=std::next(vector.begin(),-1); it--) functor(*it);
std::cout << "[Stack::pop()]: --- STACK BOTTOM ---" << WHITE << std::endl;
}
template<class T>
void amo::Stack<T>::push(T const& t) {
//std::cout << "[Stack::push()]: t:" << t << WHITE << std::endl;
vector.push_back(t);
}
template<class T>
T amo::Stack<T>::pop() {
T t = vector.at(vector.size()-1);
vector.pop_back();
//std::cout << "[Stack::pop()]: returns:" << t << WHITE << std::endl;
return t;
}
template<class T>
T& amo::Stack<T>::top() {
return vector.at(vector.size()-1);
}
}
#endif