-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLista.h
More file actions
186 lines (181 loc) · 3.46 KB
/
Copy pathLista.h
File metadata and controls
186 lines (181 loc) · 3.46 KB
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
184
#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <fstream>
#include "Base.h"
using namespace std;
template<class G>
class Nodo {
public:
G elemento;
Nodo<G>* sig;
Nodo<G>* ant;
Nodo(G elemento, Nodo<G>* sig=nullptr, Nodo<G>* ant=nullptr):elemento(elemento),sig(sig),ant(ant){}
};
template<class G>
class Lista {
private:
Nodo<G>* inicio;
Nodo<G>* fin;
int cantidad;
public:
Lista()
{
inicio = fin = nullptr;
cantidad = 0;
}
void pushFront(G e)
{
Nodo<G>* n = new Nodo<G>(e);
if (cantidad == 0)
inicio = fin = n;
else {
n->sig = inicio;
inicio->ant = n;
inicio = n;
}
++cantidad;
}
void pushBack(G elem)
{
Nodo<G>* nuevo = new Nodo<G>(elem);
if (cantidad == 0)
inicio =fin= nuevo;
else
{
fin->sig = nuevo;
nuevo->ant = fin;
fin = nuevo;
}
++cantidad;
}
G at(short i)
{
Nodo<G>* aux = inicio;
size_t cont = 0;
while (cont != i && aux != nullptr)
{
aux = aux->sig;
cont++;
}
return aux->elemento;
}
//void imprimir_con_lambdas(function<void(Generico)> imprime) {
// Nodo<Generico>* aux = inicio;
// while (aux != nullptr) {
// imprime(aux->elemento);
// aux = aux->sig;
// }
//}
/*void imprimir_recursivamente_lambdas(function<void(Generico)> imprime, Nodo<Generico>* aux) {
// if (aux == nullptr) return;
// imprime(aux->elemento);
// aux = aux->sig;
// imprimir_recursivamente_lambdas(imprime, aux);
}*/
//recursivo
void imprimirRecursivaLambdasCondicion(function<void(G)> imprime, Nodo<G>* aux, function<bool(G)> condicion)
{
if (aux == nullptr) return;
if (condicion(aux->elemento)) {
imprime(aux->elemento);
}
aux = aux->sig;
imprimirRecursivaLambdasCondicion(imprime, aux, condicion);
}
void imprimirDatos(function<void(G)> imprime, Nodo<G>* aux)
{
while (aux != nullptr)
{
imprime(aux->elemento);
aux = aux->sig;
}
}
void eliminarLambdas(function<bool(G)> condicion)
{
Nodo<G>* actual = inicio;
Nodo<G>* antes = nullptr;
while (actual != nullptr && condicion(actual->elemento))
{
antes = actual;
actual = actual->sig;
}
if (actual == nullptr)
{
cout << "No existe el valor a eliminar" << endl;
}
else if (antes == nullptr)
{
inicio = inicio->sig;
--cantidad;
delete actual;
}
else if (actual->sig == nullptr)
{
antes->sig = nullptr;
--cantidad;
fin = antes;
delete actual;
}
else
{
antes->sig = actual->sig;
--cantidad;
delete actual;
}
}
void eliminartodo() {
Nodo<Generico>* actual = inicio;
Nodo<Generico>* antes = nullptr;
do
{
antes = actual;
actual = actual->sig;
delete actual;
} while (actual != nullptr);
}
void buscarLambdas(function<bool(G)> condicion)
{
Nodo<G>* actual = inicio;
Nodo<G>* antes = nullptr;
while (actual != nullptr && condicion(actual->elemento))
{
antes = actual;
actual = actual->sig;
}
if (actual == nullptr)
{
cout << "No existe" << endl;
}
else if (antes == nullptr)
{
cout << "Se encontro" << endl;
}
else
{
cout << "Se encontro" << endl;
}
}
void ordenarLambdas(function<bool(G, G)> condicion)
{
Nodo<G>* p = inicio;
while (p != nullptr)
{
Nodo<G>* j = p->sig;
while (j != nullptr)
{
if(condicion(p->elemento, j->elemento))
{
G aux = j->elemento;
j->elemento = p->elemento;
p->elemento = aux;
}
j = j->sig;
}
p = p->sig;
}
}
Nodo<G>* retornarNodoInicio() { return inicio; }
int retornarCantidad(){ return cantidad; }
};