Skip to content

Instantly share code, notes, and snippets.

@Crepu
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save Crepu/513d721da994d9029738 to your computer and use it in GitHub Desktop.

Select an option

Save Crepu/513d721da994d9029738 to your computer and use it in GitHub Desktop.
Ejercicio de uso e implementación de listas enlazadas.
#include "nodo.h"
class Lista
{
private:
Nodo *head;
public:
Lista();
//~Lista();
bool isEmpty();
void empty();
void add(Nodo *);
void del(std::string);
void imprimir();
};
Lista::Lista()
{
this->head = NULL;
//new Nodo("NULL");
}
bool Lista::isEmpty()
{
return this->head == NULL;
}
void Lista::empty()
{
this->head = NULL;
}
void Lista::add(Nodo *nuevo)
{
if (this->head == NULL)
{
head = nuevo;
return;
}
/* Agregar al inicio */
/* nuevo->next = head;
head = nuevo;
*/
Nodo *aux = head;
while(aux->next != NULL)
{
aux = aux->next;
}
aux->next = nuevo;
}
void Lista::del(std::string borrar)
{
if(head != NULL)
{
if(head->dato == borrar)
{
head = head->next;
}
Nodo *aux = head;
while(aux->next->dato != borrar)
{
aux = aux->next;
}
aux->next = aux->next->next;
}
}
void Lista::imprimir()
{
Nodo *aux = head;
while(aux != NULL)
{
std::cout<<aux->dato<<"->";
aux = aux->next;
}
std::cout<"NULL\n";
}
#include <string>
#include <iostream>
class Nodo
{
public:
std::string dato;
Nodo *next;
Nodo(std::string);
//~Nodo();
};
Nodo::Nodo(std::string dato)
{
this->dato = dato;
next = NULL;
}
#include <iostream>
#include "lista.h"
using namespace std;
int main(int argc, char const *argv[])
{
/* code */
Lista paseo;
int opcion;
if(paseo.isEmpty()) cout<<"Mi lista esta vacia"<<endl;
cout<<"Paseo centro de alumnos"<<endl;
while(true)
{
cout<<"Ingrese 1 para inscribir personas y 2 para eliminar y 3 para salir."<<endl;
cin>>opcion;
if(opcion == 1)
{
string nombre;
cout<<"Igrese nombre del alumno: ";
cin>>nombre;
Nodo *nuevo = new Nodo(nombre);
paseo.add(nuevo);
paseo.imprimir();
}
if(opcion == 2)
{
string nombre;
cout<<"Igrese nombre del alumno: ";
cin>>nombre;
paseo.del(nombre);
paseo.imprimir();
}
if(opcion == 3)
{
paseo.imprimir();
cout<<"A Dios."<<endl;
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment