Advertisement
Gustavo_Inzunza

Pila.c

Aug 20th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct tpila
  4. {
  5.   int clave;
  6.   struct tpila *sig;
  7. };
  8. //- Procedimiento de creación:
  9.  
  10. void crear(struct tpila **pila)
  11. {
  12.   *pila = (struct tpila *) malloc(sizeof(struct tpila));
  13.   (*pila)->sig = NULL;
  14. }
  15. //- Función que devuelve verdadero si la pila está vacía:
  16.  
  17. int vacia(struct tpila *pila)
  18. {
  19.   return (pila->sig == NULL);
  20. }
  21.  
  22. //- Procedimiento de apilado (apila al comienzo de la lista):
  23.  
  24. void apilar(struct tpila *pila, int elem)
  25. {
  26.   struct tpila *nuevo;
  27.  
  28.   nuevo = (struct tpila *) malloc(sizeof(struct tpila));
  29.   nuevo->clave = elem;
  30.   nuevo->sig = pila->sig;
  31.   pila->sig = nuevo;
  32. }
  33. //- Procedimiento de desapilado (desapila del comienzo de la lista):
  34.  
  35. void desapilar(struct tpila *pila)
  36. {
  37.   struct tpila *aux;
  38.   aux = pila->sig;
  39.   pila->sig = aux->sig;
  40.   free(aux);
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.   struct tpila *pila;
  47.   int elem;
  48.   crear(&pila);
  49.   //if (vacia(pila)) printf("\nPila vacia!\n");
  50.   apilar(pila, 1);
  51.   apilar(pila, 6);
  52.   apilar(pila, 9);
  53.   printf("%d\n",(*((*pila).sig)).clave);
  54.   apilar(pila, 24);
  55.   desapilar(pila);
  56.   printf("%d\n",(pila->sig)->clave);
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement