-
Notifications
You must be signed in to change notification settings - Fork 0
/
pila.h
112 lines (96 loc) · 1.59 KB
/
pila.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
#ifndef PILA_H
#define PILA_H
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class Nodo{
private:
T Info;
Nodo<T> *ApSig;
public:
Nodo();
Nodo(T inf);
T getInfo();
void setInfo(T inf);
Nodo<T> *getApSig();
void setApSig(Nodo<T> *Ap);
};
template <typename T>
Nodo<T>::Nodo(){
ApSig = NULL;
};
template <typename T>
Nodo<T>::Nodo(T inf){
ApSig = NULL;
Info = inf;
};
template <typename T>
T Nodo<T>::getInfo(){
return Info;
};
template <typename T>
void Nodo<T>::setInfo(T inf){
Info = inf;
};
template <typename T>
Nodo<T> *Nodo<T>::getApSig(){
return ApSig;
};
template <typename T>
void Nodo<T>::setApSig(Nodo<T> *Ap){
ApSig = Ap;
};
template <typename T>
class Pila{
private:
Nodo<T> *Tope;
public:
Pila();
bool push(T inf);
bool pop(T &inf);
bool estaVacia();
int numElementos();
};
template <typename T>
Pila<T>::Pila(){
Tope = NULL;
};
template <typename T>
bool Pila<T>::push(T inf){
Nodo<T> *Ap = new Nodo<T>;
Ap->setInfo(inf);
Ap->setApSig(Tope);
Tope = Ap;
return true;
};
template <typename T>
bool Pila<T>::pop(T &inf){
if(estaVacia()){
inf = NULL;
return false;
};
inf = Tope->getInfo();
Nodo<T> *Ap = new Nodo<T>;
Ap->setApSig(Tope);
Tope = Tope->getApSig();
delete Ap;
return true;
};
template <typename T>
bool Pila<T>::estaVacia(){
return (Tope==NULL) ? true : false;
};
template <typename T>
int Pila<T>::numElementos(){
if(estaVacia()) return 0;
Nodo<T> *Ap = Tope;
int cantidad = 1;
while(1){
if(Ap->getApSig()==NULL) break;
cantidad++;
Ap = Ap->getApSig();
}
return cantidad;
};
#endif