lunes, 19 de enero de 2015

Lista Simplemente enlazada

Hola a todos, el día de hoy les traigo mi primer aporte:  código de una Lista Simplemente Enlazada, (muy simple), hecha con estructuras, clases y apuntadores en C++.
Me gustaría que analizaran el código y sus dudas por favor en los comentarios les respondo.


#include <iostream>

using namespace std;


struct nodo{
    int dato;
    nodo *sig;

    nodo(){ sig=NULL; }
    nodo(int x){ sig=NULL; dato=x; }

};

class LSE{
 private:
    nodo *inicio;

 public:
    void insertarI(int);
    void insertarF(int);
    void imprimir();


    LSE(){ inicio=NULL; }
};

void LSE::insertarI(int x){
    if(inicio==NULL)
            inicio= new nodo(x);
    else{
        nodo *foco=new nodo(x);
        foco->sig= inicio;
        inicio=foco;
    }
}

void LSE::insertarF(int x){
    if(inicio==NULL)
            inicio= new nodo(x);
    else{
        nodo *green=inicio;
        while(green->sig!=NULL){
            green = green->sig;
        }
        nodo *blue = new nodo(x);
        green->sig= blue;
    }
}

void LSE::imprimir(){
    if(inicio==NULL)
            cout << "La lista esta vacia";
    else{
        nodo *chofis=inicio;

        do{
         cout << chofis->dato << " ";
         chofis = chofis->sig;
        }
        while(chofis!=NULL);

    }
}

int main()
{
    LSE a;

    a.insertarI(8);
    a.insertarF(3);
    a.insertarI(1);

    a.imprimir();

    return 0;
}

No hay comentarios:

Publicar un comentario