Advertisement
Guest User

Pilas con Filas simples...o intento de

a guest
Sep 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct stPila
  6. {
  7. int dato;
  8. struct stPila* siguiente;
  9.  
  10. } stPila;
  11.  
  12. void inicPila(stPila* p);
  13. int pilavacia(stPila* p);
  14. int tope(stPila* p);
  15. void mostrar(stPila* p);
  16. void apilar(stPila* p, int elemento);
  17. int desapilar(stPila* p);
  18. void leer(stPila* p);
  19.  
  20.  
  21. int main()
  22. {
  23. int prueba;
  24. stPila pila;
  25. inicPila(&pila);
  26.  
  27.  
  28.  
  29. prueba=pilavacia(&pila); ///deberìa retornar un 1
  30. printf("prueba antes de apilar: %d\n", prueba);
  31. system("pause");
  32.  
  33. apilar(&pila, 5);
  34.  
  35. prueba=pilavacia(&pila); ///deberia retornar un 0
  36. printf("prueba luego de apilar: %d\n", prueba);
  37. system("pause");
  38.  
  39. printf("\nMostrando pila\n");
  40. mostrar(&pila);
  41. system("pause");
  42.  
  43. leer(&pila);
  44. mostrar(&pila);
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50. /// FUNCIONES PILA CON FILAS SIMPLES///
  51. void inicPila(stPila* p)
  52. {
  53. p = NULL;
  54. }
  55.  
  56. int pilavacia(stPila* p)
  57. {
  58. int flag=20;
  59. if (p != NULL) {flag=0;}
  60. else if (p == NULL) {flag=1;}
  61.  
  62. return flag;
  63. }
  64.  
  65. int tope(stPila* p)
  66. {
  67. int tope = p->dato;
  68. return tope;
  69. }
  70.  
  71. void mostrar(stPila* p)
  72. {
  73. stPila* aux = p;
  74.  
  75. while (!pilavacia(&aux))
  76. {
  77. printf("//tope %d//", tope(&aux));
  78. aux=aux->siguiente;
  79. }
  80. }
  81.  
  82. void apilar(stPila* p, int elemento)
  83. {
  84. int prueba=50;
  85. stPila* aux= (stPila*) malloc(sizeof(stPila));
  86. aux->dato=elemento; /** Hace falta modularizar la funcion "crear nodo"? traté de limitarme a las funciones de pila que trabajamos en el ingreso*/
  87. aux->siguiente=NULL; /** #"AlUsuarioNoLeImporta..."*/
  88.  
  89. if (!pilavacia(&p))
  90. {
  91. aux->siguiente=p; ///hago el "enganche"
  92. }
  93.  
  94. p = aux; ///fuera del if porque de cualquier modo lo voy a modificar
  95. printf("p->dato %d\n", p->dato);
  96. prueba=pilavacia(&p);
  97. printf("prueba dentro de apilar= %d", prueba);
  98. }
  99.  
  100. int desapilar(stPila* p)
  101. {
  102. int desapile= tope(&p); ///me aseguro el retorno
  103. stPila* aux = p;
  104.  
  105. p=p->siguiente; ///avanzo
  106. free(aux); ///libero el nodo
  107.  
  108. return desapile;
  109. }
  110.  
  111.  
  112. void leer(stPila* p)
  113. {
  114. int elemento;
  115. printf("Ingrese un numero entero: ");
  116. scanf("%d", &elemento);
  117. apilar(&p, elemento);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement