Lista enlazada simple: Insertar inicio

#include <iostream>
using namespace std;

struct nodo{
       int nro;      
       struct nodo *sgte;
};

typedef struct nodo *Tlista;

void insertarInicio(Tlista &lista, int valor)
{
    Tlista q;
    q = new(struct nodo);
    q->nro = valor;
    q->sgte = lista;
    lista  = q;
}

void mostrarLista(Tlista lista)
{
     int i = 0;
     while(lista != NULL)
     {
          cout <<" [" <<i+1 <<"] " << lista->nro << endl;
          lista = lista->sgte;
          i++;
     }
}


int main()
{
    Tlista lista = NULL;
    int op;    
    int dato;
    int pos;  

   do
    {
        cout<<"\n\t\tLISTA ENLAZADA SIMPLE\n\n";
    cout<<" 1. INSERTAR AL INICIO               "<<endl;
    cout<<" 2. MOSTRAR LISTA                    "<<endl;
    cout<<"\n INGRESE OPCION: ";
cin>> op;

        switch(op)
        {
            case 1:
                cout<< "\n NUMERO A INSERTAR: "; cin>> dato;
                insertarInicio(lista, dato);
            break;
         
      case 2:
                cout << "\n\n MOSTRANDO LISTA\n\n";
                mostrarLista(lista);
            break;
        }

        cout<<endl<<endl;
        system("pause");
system("cls");
       

    }while(op!=3);
 
   return 0;
}

Comentarios

Entradas más populares de este blog

Invertir número

Numero capicua

Hallar el dígito mayor de un numero