-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLista.cpp
89 lines (81 loc) · 1.94 KB
/
Lista.cpp
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
#include <typeinfo>
template<typename T>
struct Lista {
T dato;
Lista *siguiente;
};
/**
* Se encarga de insertar un nuevo elemento a la lista
*/
template<typename T>
void pushLista(Lista<T> *&lista, T dato) {
Lista<T> *nueva = new Lista<T>();
Lista<T> *temp1, *temp2;
temp1 = lista;
nueva->dato = dato;
while(temp1 != NULL) {
temp2 = temp1;
temp1 = temp1->siguiente;
}
if(lista == temp1){
lista = nueva;
}else {
temp2->siguiente= nueva;
}
nueva->siguiente = temp1;
}
/**
* Se encarga de borrar un elemento de la lista
*/
template <typename T>
void deleteLista(Lista<T> *&lista, T dato) {
if(lista != NULL) {
Lista<T> *tempBorrar;
Lista<T> *anterior = NULL;
tempBorrar = lista;
while((tempBorrar != NULL) && tempBorrar->dato == dato) {
anterior = tempBorrar;
tempBorrar = tempBorrar->siguiente;
}
if(tempBorrar == NULL) {
std::cout << std::endl << "El valor buscado no existe";
}else if(anterior == NULL) {
lista = lista->siguiente;
delete (tempBorrar);
} else {
anterior->siguiente = tempBorrar->siguiente;
delete (tempBorrar);
}
}
}
/**
* Verifica que exista el elemento especificado en la lista
*/
template <typename T>
bool isLista(Lista<T> *&lista, T dato) {
bool isIn = false;
Lista<T> *aux = lista;
while(lista != NULL) {
if(lista->dato == dato)
isIn = true;
lista = lista->siguiente;
}
lista = aux;
return isIn;
}
/**
* Obtiene un dato de la lista especificado
*/
template <typename T>
T getLista(Lista<T> *&lista, T dato) {
T resultado;
Lista<T> *aux = lista;
while(lista != NULL) {
if(lista->dato == dato) {
resultado = lista->dato;
}
lista = lista->siguiente;
}
lista = aux;
return resultado;
}