forked from schnorr/mlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpilha_generica.cc
71 lines (59 loc) · 1.18 KB
/
pilha_generica.cc
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
#include <iostream>
template <class TipoGenerico> class Pilha {
private:
TipoGenerico *stackPtr;
int maxLen;
int topSub;
public:
Pilha() {
stackPtr = new TipoGenerico [100];
maxLen = 99;
topSub = -1;
};
Pilha(int size) {
stackPtr = new TipoGenerico [size];
maxLen = size - 1;
topSub = -1;
};
~Pilha(){
delete stackPtr;
};
void empilha(TipoGenerico number) {
if (topSub == maxLen){
std::cerr << "Error in push - stack is full" << std::endl;
}else{
stackPtr[++topSub] = number;
}
};
void desempilha() {
if (vazio()){
std::cerr << "Error in pop - stack is empty" << std::endl;
}else{
topSub--;
}
};
TipoGenerico topo() {
if (vazio()){
std::cerr << "Error in top - stack is empty" << std::endl;
}else{
return stackPtr[topSub];
}
};
int vazio() {
return topSub == -1;
};
};
int main()
{
Pilha<int> pInteiro(100);
pInteiro.empilha(1);
pInteiro.desempilha();
Pilha<float> pFlutuante(100);
pFlutuante.empilha(2.3);
pFlutuante.desempilha();
Pilha<Pilha<int>* > p;
p.empilha(&pInteiro);
p.empilha(&pInteiro);
p.desempilha();
p.desempilha();
}