Skip to content

Instantly share code, notes, and snippets.

@codepainkiller
Last active March 3, 2016 22:15
Show Gist options
  • Save codepainkiller/508c65852c5f9119a60d to your computer and use it in GitHub Desktop.
Save codepainkiller/508c65852c5f9119a60d to your computer and use it in GitHub Desktop.
Pilas/Stack
/*
* C++ - Pilas/Stack
* Copyright 2014 Martin Cruz Otiniano <[email protected]>
*
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment