-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista.h
190 lines (178 loc) · 3.18 KB
/
lista.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef LISTA_H
#define LISTA_H
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class NodoL{
private:
T Info;
NodoL<T> *ApAnt;
NodoL<T> *ApSig;
public:
NodoL();
NodoL(T inf);
T getInfo();
NodoL<T> *getApAnt();
NodoL<T> *getApSig();
void setInfo(T inf);
void setApAnt(NodoL<T> *ApA);
void setApSig(NodoL<T> *ApS);
};
template <typename T>
NodoL<T>::NodoL(){
ApAnt = ApSig = NULL;
};
template <typename T>
NodoL<T>::NodoL(T inf){
Info = inf;
ApAnt = ApSig = NULL;
};
template <typename T>
T NodoL<T>::getInfo(){
return Info;
};
template <typename T>
NodoL<T> *NodoL<T>::getApAnt(){
return ApAnt;
};
template <typename T>
NodoL<T> *NodoL<T>::getApSig(){
return ApSig;
};
template <typename T>
void NodoL<T>::setInfo(T inf){
Info = inf;
};
template <typename T>
void NodoL<T>::setApAnt(NodoL<T> *ApA){
ApAnt = ApA;
};
template <typename T>
void NodoL<T>::setApSig(NodoL<T> *ApS){
ApSig = ApS;
};
template <typename T>
class Lista{
private:
NodoL<T> *ApInicio;
NodoL<T> *ApFin;
int NumElem;
public:
Lista();
bool pushIni(T inf);
bool pushFin(T inf);
bool popIni(T &inf);
bool popFin(T &inf);
bool estaVacia();
int numElem();
bool contiene(T inf);
T getInfoAt(int i);
};
template <typename T>
Lista<T>::Lista(){
ApFin = ApInicio = NULL;
NumElem = 0;
};
template <typename T>
bool Lista<T>::pushIni(T inf){
NodoL<T> *Ap = new NodoL<T>;
Ap->setInfo(inf);
if(estaVacia()){
ApFin = ApInicio = Ap; NumElem++;
return true;
}
Ap->setApSig(ApInicio);
Ap->getApSig()->setApAnt(Ap);
ApInicio = Ap;
NumElem++;
return true;
};
template <typename T>
bool Lista<T>::pushFin(T inf){
NodoL<T> *Ap = new NodoL<T>;
Ap->setInfo(inf);
if(estaVacia()){
ApFin = ApInicio = Ap; NumElem++;
return true;
}
Ap->setApAnt(ApFin);
Ap->getApAnt()->setApSig(Ap);
ApFin = Ap;
NumElem++;
return true;
};
template <typename T>
bool Lista<T>::popIni(T &inf){
if(estaVacia()){
inf = NULL;
return false;
}
NodoL<T> *ApAux = new NodoL<T>;
ApAux = ApInicio;
inf = ApAux->getInfo();
if(NumElem!=1){
ApInicio = ApInicio->getApSig();
ApInicio->setApAnt(NULL);
}
if(NumElem==1){
ApInicio = ApFin = NULL;
}
delete ApAux;
NumElem--;
return true;
};
template <typename T>
bool Lista<T>::popFin(T &inf){
if(estaVacia()){
inf = NULL;
return false;
}
NodoL<T> *ApAux = new NodoL<T>;
ApAux = ApFin;
inf = ApAux->getInfo();
if(NumElem!=1){
ApFin = ApFin->getApAnt();
ApFin->setApSig(NULL);
}
if(NumElem==1){
ApInicio = ApFin = NULL;
}
delete ApAux;
NumElem--;
return true;
};
template <typename T>
bool Lista<T>::estaVacia(){
if(ApInicio==NULL && ApFin==NULL) return true;
return false;
};
template <typename T>
int Lista<T>::numElem(){
return NumElem;
};
template <typename T>
bool Lista<T>::contiene(T inf){
T InfoAux = inf;
NodoL<T> *ApAux;
ApAux = ApInicio;
for(int i=0; i<NumElem; i++){
if(ApAux->getInfo()==InfoAux) return true;
ApAux = ApAux->getApSig();
if(ApAux==NULL) break;
};
return false;
};
template <typename T>
T Lista<T>::getInfoAt(int i){
NodoL<T> *Ap = NULL;
if(i<0 || i>=NumElem) return NULL;
int j = 0;
Ap = ApInicio;
while(j!=i){
Ap = Ap->getApSig();
j++;
}
return Ap->getInfo();
};
#endif