Professional Documents
Culture Documents
cap=029
Imaginemos un mundo libre
El blog de Ronny, dedicado a la libertad de acceso al conocimiento.
imprimir?
01
02
03
04
05
06
07
08
09
10
En cristiano:
55-> 60-> 31-> 5-> 4-> 51-> 9-> 27-> 68-> 62-> NULL
Nodo->
Nodo->
Nodo->
Nodo->
Nodo->
Nodo->
Internamente:
Dato: 55 Direcion:
Dato: 60 Direcion:
Dato: 31 Direcion:
Dato: 5 Direcion:
Dato: 4 Direcion:
Dato: 51 Direcion:
0x3d2c00
0x3d2c80
0x3d2c90
0x3d2ca0
0x3d2cb0
0x3d2cc0
Siguiente:
Siguiente:
Siguiente:
Siguiente:
Siguiente:
Siguiente:
0x3d2c80
0x3d2c90
0x3d2ca0
0x3d2cb0
0x3d2cc0
0x3d3ab8
11
12
13
14
Nodo->
Nodo->
Nodo->
Nodo->
Dato:
Dato:
Dato:
Dato:
9
27
68
62
Direcion:
Direcion:
Direcion:
Direcion:
0x3d3ab8
0x3d3ac8
0x3d3ad8
0x3d3ae8
Siguiente:
Siguiente:
Siguiente:
Siguiente:
0x3d3ac8
0x3d3ad8
0x3d3ae8
0
Imprimir la lista
Buscar un elemento
Podrn ver la variable *temp en casi todos los mtodos , este es el puntero de tipo Nodo
que me va a permitir moverme a travs de la lista y que inicialmente es igual a la cabeza
(head). Mientras exista algo en la lista, voy avanzado el puntero para que apunte al
siguiente. Esto se consigue en casi todos los casos con un while.
While(temp)
temp = temp->gte
Otra operacin comn en los mtodos es preguntar si inicialmente la lista est vaca, es
decir, si la cabeza no contiene algo o es igual a Null.
if(!head) o if(head==NULL)
Apliqu mis limitados conocimientos de templates para tener una lista genrica y as
pueda funcionar con varios tipos de datos y de verdad funciona.
Ah la definicin e implementacin de la clase, lista, clase nodo y el main para ver el
funcionamiento. Cualquier crtica, sugerencia o comentarios son bienvenidos siempre.
Nodo.h
ver fuente
imprimir?
01
02
03
04
05
06
#ifndef NODO_H
#define NODO_H
#include <iostream>
using namespace std;
template <class T>
07
08
09
10
11
12
13
14
15
16
17
18
19
20
class Nodo
{
public:
T dato;
Nodo *sgte;
Nodo();
Nodo(T);
~Nodo();
void print();
void eliminar();
};
#endif // NODO_H
Nodo.cpp
ver fuente
imprimir?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Nodo.h"
//constructor por defecto
template<typename T>
Nodo<T>::Nodo()
{
dato = NULL;
sgte = NULL;
}
//constructor por parametro
template<typename T>
Nodo<T>::Nodo(T dato_)
{
dato = dato_;
sgte = NULL;
}
//imprimir un nodo
template<typename T>
void Nodo<T>::print()
{
//cout << "Nodo-> " << "Dato: " << dato << " Direcion: " << this
23
<< " Siguiente: " << sgte << endl;
24
cout << dato << "-> ";
25
}
26
27
//eliminar todos los nodos
28
template<typename T>
29
void Nodo<T>::eliminar()
30
{
31
32
33
34
35
36
37
if(sgte)
sgte->eliminar();
delete this;
}
template<typename T>
Nodo<T>::~Nodo(){}
Lista.h
ver fuente
imprimir?
01
02
03
04
05
06
07
08
09
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
#ifndef LISTA_H
#define LISTA_H
#include <iostream>
#include "time.h"
#include <fstream>
#include <string>
#include "Nodo.h"
#include "Nodo.cpp"
using namespace std;
template <class T>
class Lista
{
private:
int dim,num_nodos;
T line;
string archivo;
public:
T ele;
Nodo<T> *head;
Lista();
~Lista();
void llenar_teclado(int);
void llenar_aleatorio(int);
void print();
void add_end(T);
void add_head(T);
void add_sort(T);
void search(T);
void del_dato(T);
void del_pos(int);
void del_all();
void invertir();
39
40
41
42
43
44
45
46
47
void sort();
void load(string);
void save(string);
void concat(Lista);
void interseccion(Lista);
};
#endif // LISTA_H
Lista.cpp
ver fuente
imprimir?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
#include "Lista.h"
using namespace std;
//constructor por defecto
template<typename T>
Lista<T>::Lista()
{
num_nodos = 0;
head = NULL;
}
//llenar la lista por teclado
template<typename T>
void Lista<T>::llenar_teclado(int dim)
{
for(int i=0;i<dim;i++){
cout << "Ingresa el elemento " << i + 1 << endl;
cin >> ele;
add_end(ele);
}
}
//llenar la lista aleatoriamente para enteros
template<typename T>
void Lista<T>::llenar_aleatorio(int dim)
{
srand(time(NULL));
for(int i=0;i<dim;i++){
add_end(rand() % 100);
}
}
//imprimir la lista
template<typename T>
void Lista<T>::print()
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
{
Nodo<T> *temp = head;
if(!head){
cout << "La lista esta vacia " << endl;
} else{
while(temp){
temp->print();
if(!temp->sgte) cout << "NULL";
temp = temp->sgte;
}
}
cout << endl << endl;
}
//insertar al final
template<typename T>
void Lista<T>::add_end(T dato_)
{
Nodo<T> *temp = head;
Nodo<T> *nuevo = new Nodo<T> (dato_);
if(!head){
head = nuevo;
} else{
while(temp->sgte !=NULL){
temp = temp->sgte;
}
temp->sgte = nuevo;
}
num_nodos++;
}
//insertar al inicio
template<typename T>
void Lista<T>::add_head(T dato_)
{
Nodo<T> *temp = head;
Nodo<T> *nuevo = new Nodo<T> (dato_);
if(!head){
head = nuevo;
} else{
nuevo->sgte = head;
head = nuevo;
while(temp){
temp = temp->sgte;
}
}
num_nodos++;
}
//insertar de manera ordenada
template<typename T>
090
091
092
093
094
095
096
097
098
099
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
184
185
186
187
188
189
190
191
192
193
194
if(head->dato == dato_){
head = temp->sgte;
} else {
while(temp1){
if(temp1->dato == dato_){
Nodo<T> *aux = temp1;
temp->sgte = temp1->sgte;
delete aux;
cont++;
num_nodos--;
}
temp = temp->sgte;
temp1 = temp1->sgte;
}
}
if(cont == 0){
cout << "No existe el dato " << endl;
}
}
//eliminar por posicion del nodo
template<typename T>
void Lista<T>::del_pos(int pos)
{
Nodo<T> *temp = head;
Nodo<T> *temp1 = temp->sgte;
if(pos < 1 || pos > num_nodos){
cout << "Fuera de rango " << endl;
} else if(pos==1){
head = temp->sgte;
} else{
for(int i=2;i<=pos;i++){
if(i==pos){
Nodo<T> *aux = temp1;
temp->sgte = temp1->sgte;
delete aux;
num_nodos--;
}
temp = temp->sgte;
temp1 = temp1->sgte;
}
}
}
//eliminar todos los nodos
template<typename T>
void Lista<T>::del_all()
{
head->eliminar();
head = 0;
}
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//invertir la lista
template<typename T>
void Lista<T>::invertir()
{
Nodo<T> *temp = head;
Nodo<T> *prev = NULL;
Nodo<T> *next = NULL;
while(temp){
next = temp->sgte;
temp->sgte = prev;
prev = temp;
temp = next;
}
head = prev;
}
//ordenar de manera ascendente
template<typename T>
void Lista<T>::sort()
{
T aux2;
Nodo<T> *aux = head;
Nodo<T> *temp = aux;
while(aux){
temp=aux;
while(temp->sgte){
temp=temp->sgte;
if(aux->dato>temp->dato){
aux2=aux->dato;
aux->dato=temp->dato;
temp->dato=aux2;
}
}
aux=aux->sgte;
}
}
//cargar una lista de un archivo
template<typename T>
void Lista<T>::load(string archivo)
{
ifstream in;
in.open(archivo.c_str());
if(!in.is_open()){
cout << "No se puede abrir el archivo: " << archivo << endl <<
endl;
} else{
while(in >> line){
add_end(line);
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
}
}
in.close();
}
//guardar una lista en un archivo
template<typename T>
void Lista<T>::save(string archivo)
{
Nodo<T> *temp = head;
ofstream out;
out.open(archivo.c_str());
if(!out.is_open()){
cout << "No se puede guardar el archivo " << endl;
} else{
while(temp){
out << temp->dato;
out << " ";
temp = temp->sgte;
}
}
out.close();
}
//concatenar a otra lista
template<typename T>
void Lista<T>::concat(Lista Lista2)
{
Nodo<T> *temp2 = Lista2.head;
while(temp2){
add_end(temp2->dato);
temp2 = temp2->sgte;
}
}
//numeros que coinciden en 2 listas
template<typename T>
void Lista<T>::interseccion(Lista Lista2)
{
Nodo<T> *temp = head;
Nodo<T> *temp2 = Lista2.head;//cabeza de la segunda lista
Lista lista_interseccion;//creo otra lista
int num_nodos2 = Lista2.num_nodos;//nodos de la segunda lista
int num_inter=0;//numero de coincidencias
//creo 2 vectores dinamicos
T *v1 = new T[num_nodos];
T *v2 = new T[num_nodos2];
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
for(int i=0;i<tam;i++){
for(int j=i-1;j >=0 && a[j+1] < a[j];j--){
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
template<typename T>
Lista<T>::~Lista(){}
main.cpp
ver fuente
imprimir?
01
02
#include <iostream>
#include "Lista.h"
#include "Lista.cpp" //por errores de linking de tipo "undefined
03
reference to" (estudiando)
04
05
using namespace std;
06
int main()
07
{
08
Lista<int> l1;
09
Lista<int> l2;
10
11
int dim,pos;
12
string archivo;
13
14
cout << "Ingresa la dimension de la lista " << endl;
15
cin >>dim;
16
17
l1.llenar_aleatorio(dim);//llenar_teclado para otros tipos
18
19
cout << "Lista A al inicio " << endl;
20
l1.print();
21
22
cout << "Agrega un elemento por la cabeza" << endl;
23
cin >>l1.ele;
24
l1.add_head(l1.ele);
25
l1.print();
26
27
cout << "Lista invertida " << endl;
28
l1.invertir();
29
l1.print();
30
31
cout << "Lista ordenada " << endl;
32
l1.sort();
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
l1.print();
cout << "Agrega un elemento (Sera ordenado)" << endl;
cin >>l1.ele;
l1.add_sort(l1.ele);
l1.print();
cout << "Busca un elemento" << endl;
cin >>l1.ele;
l1.search(l1.ele);
cout << "Elimina un elemento por dato" << endl;
cin >>l1.ele;
l1.del_dato(l1.ele);
l1.print();
cout << "Elimina un elemento por posicion" << endl;
cin >>pos;
l1.del_pos(pos);
l1.print();
cout << "Cargar una lista desde archivo - Ingresa el nombre" <<
endl;
cin >> archivo;//debe estar en el mismo directorio que este
programa
l2.load(archivo);
cout << "Lista B" << endl;
l2.print();
imprimir?
01
02
03
04
05
06
07
08
09
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
Listas A y B concatenadas
4-> 5-> 9-> 18-> 27-> 31-> 45-> 51-> 55-> 62-> 8-> 59-> 23-> 15->
44
94-> 63-> 9->17-> 62-> 33-> NULL
45
46
La lista esta vacia.