Advertisement
arthurassuncao

Pilha com vetor

Oct 29th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. //============= ARQUIVO PILHA.H ================
  2.  
  3. #ifndef PILHA_H
  4. #define PILHA_H
  5. #define MAX_TAM 1000 //tamanho qualquer predefinido
  6. typedef int apontador;
  7. typedef char tipo_chave;
  8. typedef struct{
  9.     tipo_chave chave;
  10. }tipo_item;
  11. typedef struct{
  12.     tipo_item elementos[MAX_TAM];
  13.     apontador topo;
  14. }tipo_pilha;
  15. typedef tipo_pilha *pilha;
  16.  
  17. pilha cria_pilha();
  18. void termina_pilha(pilha *p);
  19. int pilha_cheia(pilha p);
  20. int pilha_vazia(pilha p);
  21. int pop(pilha p, tipo_item *e); //retira um item da pilha e armazena seu valor em *e
  22. int push(pilha p, tipo_item e);  //coloca o item "e" na pilha
  23.  
  24. #endif //fim PILHA_H
  25.  
  26. //========== FIM DO ARQUIVO PILHA.H ================
  27.  
  28. //========== ARQUIVO PILHA.C ===============
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "pilha.h"
  33.  
  34. pilha cria_pilha()
  35. {
  36.     pilha p = (pilha)malloc(sizeof(tipo_pilha));
  37.     if (!p)
  38.         return 0;
  39.     p->topo = -1; //pilha vazia
  40.     return p;
  41.  
  42. }
  43. void termina_pilha(pilha *p)
  44. {
  45.     free(*p);
  46. }
  47. int pilha_cheia(pilha p)
  48. {
  49.     return p->topo == MAX_TAM - 1;
  50. }
  51. int pilha_vazia(pilha p)
  52. {
  53.     return p->topo==-1;
  54. }
  55. int pop(pilha p, tipo_item *e) //desempilha
  56. {
  57.     if (p->topo == -1) //pilha vazia
  58.         return 0;
  59.     *e = p->elementos[p->topo];
  60.     p->topo--;
  61.     return 1;
  62. }
  63. int push(pilha p, tipo_item e) //empilha
  64. {
  65.     if (p->topo == MAX_TAM -1) //ta cheia
  66.         return 0;
  67.     p->topo++;
  68.     p->elementos[p->topo] = e;
  69.     return 1;
  70. }
  71.  
  72. // ============ FIM DO ARQUIVO PILHA.C ================
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement