Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int TipoChave;
  5.  
  6. typedef struct {
  7. int Chave;
  8. } TipoItem;
  9.  
  10. typedef struct Celula_str *Apontador;
  11.  
  12. typedef struct Celula_str {
  13. TipoItem Item;
  14. Apontador Prox;
  15. } Celula;
  16.  
  17. typedef struct {
  18. Apontador Fundo, Topo;
  19. int Tamanho;
  20. } TipoPilha;
  21.  
  22. void FPVazia(TipoPilha *Pilha){ //Faz a pilha ficar vazia.
  23. /*Pilha->Topo = (Apontador) malloc(sizeof(Celula));
  24. Pilha->Fundo = Pilha->Topo;
  25. Pilha->Topo->Prox = NULL;
  26. Pilha->Tamanho = 0;*/
  27. Pilha->Topo->Prox = NULL;
  28. Pilha->Topo = NULL;
  29. Pilha->Fundo = Pilha->Topo;
  30. Pilha->Tamanho = 0;
  31. }
  32.  
  33. int Vazia(TipoPilha Pilha){ //Retorna true se a pilha está vazia; caso contrário, retorna false.
  34. return (Pilha.Topo == Pilha.Fundo);
  35. }
  36.  
  37. void Empilha(TipoItem x, TipoPilha *Pilha){ //Empilha(x, Pilha). Insere o item x no topo da pilha.
  38.  
  39. if(Vazia){
  40. Pilha->Fundo = Pilha->Topo;
  41. }
  42. Apontador Aux;
  43. Aux = (Apontador) malloc(sizeof(Celula));
  44. Aux->Item = x;
  45. Pilha->Topo = Aux;
  46. Pilha->Topo->Prox = NULL;
  47. Pilha->Tamanho++;
  48. /*Apontador Aux;
  49. Aux = (Apontador) malloc(sizeof(Celula));
  50. Pilha->Topo->Item = x;
  51. Aux->Prox = Pilha->Topo;
  52. Pilha->Topo = Aux;
  53. Pilha->Tamanho++;*/
  54. }
  55.  
  56. void Desempilha(TipoPilha *Pilha, TipoItem *Item){ //Retorna o item x no topo da pilha, retirando-o da pilha.
  57.  
  58.  
  59. Apontador q;
  60. if (Vazia(*Pilha)){
  61. printf(" Erro: lista vazia\n");
  62. return;
  63. }
  64. q = Pilha->Topo;
  65. Pilha->Topo = q->Prox;
  66. *Item = q->Prox->Item;
  67. free(q);
  68. Pilha->Tamanho--;
  69. }
  70.  
  71. int Tamanho(TipoPilha Pilha){ //Esta função retorna o número de itens da pilha.
  72. return (Pilha.Tamanho);
  73. }
  74.  
  75. int main(){
  76.  
  77. TipoPilha *Pilha;
  78. FPVazia(Pilha);
  79. TipoItem *Item;
  80. Item->Chave = 2;
  81. Empilha(Item, Pilha);
  82. Desempilha(Pilha, Item);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement