juan_de99

Untitled

Oct 22nd, 2021
626
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// .h pila estatica
  2.  
  3.  
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define TAM 200
  8. #define MIN(x,y) ((x)<(y)?(x):(y))
  9.  
  10. typedef struct
  11. {
  12.    char pila[TAM];
  13.    unsigned tope;
  14. }t_pila;
  15.  
  16. void crearPila(t_pila *p);
  17. int desapilar(t_pila *p,unsigned tam,void *dato);
  18. int apilar(t_pila *p,unsigned tam,const void *dato);
  19. int verTope(const t_pila *p,unsigned tam,void *dato);
  20. int pilaVacia(const t_pila *p);
  21. int pilaLlena(const t_pila *p, unsigned tam);
  22. void vaciarPila(t_pila *p);
  23.  
  24. /// .c
  25.  
  26. void crearPila(t_pila *p)
  27. {
  28.     p->tope=TAM;
  29. }
  30. int pilaVacia(const t_pila *p)
  31. {
  32.     return TAM==p->tope?1:0;
  33. }
  34. int pilaLlena(const t_pila *p, unsigned tam)
  35. {
  36.     return p->tope < (tam+sizeof(unsigned));
  37. }
  38. void vaciarPila(t_pila *p)
  39. {
  40.     p->tope=TAM;
  41. }
  42. int apilar(t_pila *p,unsigned tam,const void *dato)
  43. {
  44.  
  45.     if(p->tope < (tam+sizeof(unsigned)))
  46.         return -1;
  47.     p->tope-=tam;
  48.     memcpy(p->pila+p->tope,dato,tam);
  49.  
  50.     p->tope-=sizeof(unsigned);
  51.     memcpy(p->pila+p->tope,&tam,sizeof(unsigned));
  52.  
  53.     return 1;
  54.  
  55. }
  56. int desapilar(t_pila *p,unsigned tam,void *dato)
  57. {
  58.     unsigned tamPila;
  59.  
  60.     if(p->tope==TAM)
  61.         return -1;
  62.  
  63.     memcpy(&tamPila,p->pila+p->tope,sizeof(unsigned));
  64.  
  65.     p->tope+=sizeof(unsigned);
  66.  
  67.     memcpy(dato,p->pila+p->tope,MIN(tam,tamPila));
  68.  
  69.     p->tope+=tamPila;
  70.  
  71.     return 1;
  72. }
  73.  
  74. int verTope(const t_pila *p,unsigned tam,void *dato)
  75. {
  76. //    unsigned tamPila;
  77.  
  78.     if(p->tope==TAM)
  79.         return -1;
  80.  
  81.     memcpy(dato,p->pila+p->tope+sizeof(unsigned),MIN(tam,*((unsigned*)(p->pila+p->tope))));
  82. //    memcpy(&tamPila,p->pila+p->tope,sizeof(unsigned));
  83. //    memcpy(dato,p->pila+p->tope+sizeof(unsigned),MIN(tam,tamPila));
  84.  
  85.     return 1;
  86. }
RAW Paste Data