Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active April 22, 2021 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5c5a40dafe95260ecb57b62c4e9967e0 to your computer and use it in GitHub Desktop.
Save parzibyte/5c5a40dafe95260ecb57b62c4e9967e0 to your computer and use it in GitHub Desktop.
class Lista
{
private:
void agregarRecursivo(Nodo *n, int dato)
{
if (n->siguiente == NULL)
{
n->siguiente = new Nodo(dato);
}
else
{
this->agregarRecursivo(n->siguiente, dato);
}
}
void imprimirRecursivo(Nodo *n)
{
if (n != NULL)
{
std::cout << "Tenemos " << n->dato << std::endl;
this->imprimirRecursivo(n->siguiente);
}
}
void eliminarRecursivo(Nodo *n, int dato)
{
if (n == NULL)
{
return;
}
if (n->dato == dato && n == this->cabeza)
{
Nodo *temporal = this->cabeza;
if (this->cabeza->siguiente != NULL)
{
this->cabeza = this->cabeza->siguiente;
delete temporal;
}
else
{
this->cabeza = NULL;
}
return;
}
if (n->siguiente != NULL && n->siguiente->dato == dato)
{
Nodo *temporal = n->siguiente;
if (n->siguiente != NULL)
{
n->siguiente = n->siguiente->siguiente;
}
delete temporal;
}
else
{
this->eliminarRecursivo(n->siguiente, dato);
}
}
bool existeRecursivo(Nodo *n, int dato)
{
if (n == NULL)
{
return false;
}
if (n->dato == dato)
{
return true;
}
return this->existeRecursivo(n->siguiente, dato);
}
public:
Nodo *cabeza;
void copiaSinDuplicados(Lista *l)
{
Nodo *temporal = this->cabeza;
while (temporal != NULL)
{
if (!l->existe(temporal->dato))
{
l->agregar(temporal->dato);
}
temporal = temporal->siguiente;
}
}
void eliminar(int dato)
{
this->eliminarRecursivo(this->cabeza, dato);
}
void agregar(int dato)
{
if (this->cabeza == NULL)
{
this->cabeza = new Nodo(dato);
}
else
{
this->agregarRecursivo(this->cabeza, dato);
}
}
void imprimir()
{
std::cout << "Imprimiendo " << std::endl;
this->imprimirRecursivo(this->cabeza);
}
bool existe(int dato)
{
return this->existeRecursivo(this->cabeza, dato);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment