Skip to content

Instantly share code, notes, and snippets.

@codepainkiller
Last active March 3, 2016 22:15

Revisions

  1. @martincruz-bx martincruz-bx revised this gist Jul 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pilas.cpp
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * C++ - Pilas/Stack
    * Copyright 2014 Martin Cruz Otiniano
    * Description : Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
    * Site : www.marcsdev.com
    * Site : martincruz.me
    */

    #include <iostream>
  2. @martincruz-bx martincruz-bx revised this gist Nov 22, 2014. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions pilas.cpp
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,8 @@
    /*
    * C++ - Pilas/Stack
    * Copyright 2014 Martin Cruz Otiniano <slayer.dmx@gmail.com>
    *
    * Description: Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
    * Site: casicodigo.blogspot.com
    * Copyright 2014 Martin Cruz Otiniano
    * Description : Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
    * Site : www.marcsdev.com
    */

    #include <iostream>
  3. @martincruz-bx martincruz-bx revised this gist Oct 4, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pilas.cpp
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    * Description: Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
    * Site: casicodigo.blogspot.com
    */


    #include <iostream>
    using namespace std;
  4. @martincruz-bx martincruz-bx revised this gist Oct 4, 2014. 1 changed file with 33 additions and 33 deletions.
    66 changes: 33 additions & 33 deletions pilas.cpp
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    */


    #include &lt;iostream&gt;
    #include <iostream>
    using namespace std;

    struct nodo{
    @@ -18,77 +18,77 @@ struct nodo{
    typedef nodo *ptrPila; // creando nodo tipo puntero( tipo de dato )

    /* Apilar elemento
    ---------------------------------------------------------------------*/
    void push( ptrPila &amp;p, int valor )
    ------------------------------------------------------------------------*/
    void push( ptrPila &p, int valor )
    {
    ptrPila aux;
    aux = new(struct nodo); // apuntamos al nuevo nodo creado
    aux-&gt;nro = valor;
    aux->nro = valor;

    aux-&gt;sgte = p ;
    aux->sgte = p ;
    p = aux ;
    }

    /* Desapilar elemento(devuelve elemento)
    ---------------------------------------------------------------------*/
    int pop( ptrPila &amp;p )
    ------------------------------------------------------------------------*/
    int pop( ptrPila &p )
    {
    int num ;
    ptrPila aux;

    aux = p ;
    num = aux-&gt;nro; // asignamos el primer vamor de la pila
    num = aux->nro; // asignamos el primer vamor de la pila

    p = aux-&gt;sgte ;
    p = aux->sgte ;
    delete(aux);

    return num;
    }

    /* Muestra elementos de la pila
    ---------------------------------------------------------------------*/
    ------------------------------------------------------------------------*/
    void mostrar_pila( ptrPila p )
    {
    ptrPila aux;
    aux = p; // apunta al inicio de la lista

    while( aux !=NULL )
    {
    cout&lt;&lt;"\t"&lt;&lt; aux-&gt;nro &lt;&lt;endl;
    aux = aux-&gt;sgte;
    cout<<"\t"<< aux->nro <<endl;
    aux = aux->sgte;
    }
    }

    /* Eliminar todos los elementos de la pila
    ---------------------------------------------------------------------*/
    void destruir_pila( ptrPila &amp;p)
    ------------------------------------------------------------------------*/
    void destruir_pila( ptrPila &p)
    {
    ptrPila aux;

    while( p != NULL)
    {
    aux = p;
    p = aux-&gt;sgte;
    p = aux->sgte;
    delete(aux);
    }
    }

    /* Menu de opciones
    ----------------------------------------------------------------------*/
    ------------------------------------------------------------------------*/
    void menu()
    {
    cout&lt;&lt;"\n\t IMPLEMENTACION DE PILAS EN C++\n\n";
    cout&lt;&lt;" 1. APILAR "&lt;&lt;endl;
    cout&lt;&lt;" 2. DESAPILAR "&lt;&lt;endl;
    cout&lt;&lt;" 3. MOSTRAR PILA "&lt;&lt;endl;
    cout&lt;&lt;" 4. DESTRUIR PILA "&lt;&lt;endl;
    cout&lt;&lt;" 5. SALIR "&lt;&lt;endl;

    cout&lt;&lt;"\n INGRESE OPCION: ";
    cout<<"\n\t IMPLEMENTACION DE PILAS EN C++\n\n";
    cout<<" 1. APILAR "<<endl;
    cout<<" 2. DESAPILAR "<<endl;
    cout<<" 3. MOSTRAR PILA "<<endl;
    cout<<" 4. DESTRUIR PILA "<<endl;
    cout<<" 5. SALIR "<<endl;

    cout<<"\n INGRESE OPCION: ";
    }

    /* Funcion Principal
    ----------------------------------------------------------------------*/
    ------------------------------------------------------------------------*/
    int main()
    {
    ptrPila p = NULL; // creando pila
    @@ -100,44 +100,44 @@ int main()

    do
    {
    menu(); cin&gt;&gt; op;
    menu(); cin>> op;

    switch(op)
    {
    case 1:

    cout&lt;&lt; "\n NUMERO A APILAR: "; cin&gt;&gt; dato;
    cout<< "\n NUMERO A APILAR: "; cin>> dato;
    push( p, dato );
    cout&lt;&lt;"\n\n\t\tNumero " &lt;&lt; dato &lt;&lt; " apilado...\n\n";
    cout<<"\n\n\t\tNumero " << dato << " apilado...\n\n";
    break;


    case 2:

    x = pop( p );
    cout&lt;&lt;"\n\n\t\tNumero "&lt;&lt; x &lt;&lt;" desapilado...\n\n";
    cout<<"\n\n\t\tNumero "<< x <<" desapilado...\n\n";
    break;


    case 3:

    cout &lt;&lt; "\n\n MOSTRANDO PILA\n\n";
    cout << "\n\n MOSTRANDO PILA\n\n";
    if(p!=NULL)
    mostrar_pila( p );
    else
    cout&lt;&lt;"\n\n\tPila vacia..!"&lt;&lt;endl;
    cout<<"\n\n\tPila vacia..!"<<endl;
    break;


    case 4:

    destruir_pila( p );
    cout&lt;&lt;"\n\n\t\tPila eliminada...\n\n";
    cout<<"\n\n\t\tPila eliminada...\n\n";
    break;

    }

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

    }while(op!=5);
  5. @martincruz-bx martincruz-bx created this gist Oct 4, 2014.
    147 changes: 147 additions & 0 deletions pilas.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    /*
    * C++ - Pilas/Stack
    * Copyright 2014 Martin Cruz Otiniano <slayer.dmx@gmail.com>
    *
    * Description: Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
    * Site: casicodigo.blogspot.com
    */


    #include &lt;iostream&gt;
    using namespace std;

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

    typedef nodo *ptrPila; // creando nodo tipo puntero( tipo de dato )

    /* Apilar elemento
    ---------------------------------------------------------------------*/
    void push( ptrPila &amp;p, int valor )
    {
    ptrPila aux;
    aux = new(struct nodo); // apuntamos al nuevo nodo creado
    aux-&gt;nro = valor;

    aux-&gt;sgte = p ;
    p = aux ;
    }

    /* Desapilar elemento(devuelve elemento)
    ---------------------------------------------------------------------*/
    int pop( ptrPila &amp;p )
    {
    int num ;
    ptrPila aux;

    aux = p ;
    num = aux-&gt;nro; // asignamos el primer vamor de la pila

    p = aux-&gt;sgte ;
    delete(aux);

    return num;
    }

    /* Muestra elementos de la pila
    ---------------------------------------------------------------------*/
    void mostrar_pila( ptrPila p )
    {
    ptrPila aux;
    aux = p; // apunta al inicio de la lista

    while( aux !=NULL )
    {
    cout&lt;&lt;"\t"&lt;&lt; aux-&gt;nro &lt;&lt;endl;
    aux = aux-&gt;sgte;
    }
    }

    /* Eliminar todos los elementos de la pila
    ---------------------------------------------------------------------*/
    void destruir_pila( ptrPila &amp;p)
    {
    ptrPila aux;

    while( p != NULL)
    {
    aux = p;
    p = aux-&gt;sgte;
    delete(aux);
    }
    }

    /* Menu de opciones
    ----------------------------------------------------------------------*/
    void menu()
    {
    cout&lt;&lt;"\n\t IMPLEMENTACION DE PILAS EN C++\n\n";
    cout&lt;&lt;" 1. APILAR "&lt;&lt;endl;
    cout&lt;&lt;" 2. DESAPILAR "&lt;&lt;endl;
    cout&lt;&lt;" 3. MOSTRAR PILA "&lt;&lt;endl;
    cout&lt;&lt;" 4. DESTRUIR PILA "&lt;&lt;endl;
    cout&lt;&lt;" 5. SALIR "&lt;&lt;endl;

    cout&lt;&lt;"\n INGRESE OPCION: ";
    }

    /* Funcion Principal
    ----------------------------------------------------------------------*/
    int main()
    {
    ptrPila p = NULL; // creando pila
    int dato;
    int op;
    int x ; // numero que devuelve la funcon pop

    system("color 0b");

    do
    {
    menu(); cin&gt;&gt; op;

    switch(op)
    {
    case 1:

    cout&lt;&lt; "\n NUMERO A APILAR: "; cin&gt;&gt; dato;
    push( p, dato );
    cout&lt;&lt;"\n\n\t\tNumero " &lt;&lt; dato &lt;&lt; " apilado...\n\n";
    break;


    case 2:

    x = pop( p );
    cout&lt;&lt;"\n\n\t\tNumero "&lt;&lt; x &lt;&lt;" desapilado...\n\n";
    break;


    case 3:

    cout &lt;&lt; "\n\n MOSTRANDO PILA\n\n";
    if(p!=NULL)
    mostrar_pila( p );
    else
    cout&lt;&lt;"\n\n\tPila vacia..!"&lt;&lt;endl;
    break;


    case 4:

    destruir_pila( p );
    cout&lt;&lt;"\n\n\t\tPila eliminada...\n\n";
    break;

    }

    cout&lt;&lt;endl&lt;&lt;endl;
    system("pause"); system("cls");

    }while(op!=5);


    return 0;
    }