Herowaree

Pilha_Sequencial.c

Nov 19th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX 100
  3.  
  4. struct Pilha{
  5. int vetor[MAX];
  6. int topo;
  7. };
  8.  
  9. void iniciaPilha(struct Pilha *pilha){
  10. pilha->topo = -1;
  11. }
  12.  
  13. int vaziaPilha(struct Pilha *pilha){
  14. //O indice do topo da pilha é utilizado como valor para checar o tamanho
  15. if(pilha->topo == -1){
  16. return 1;
  17. }else{
  18. return 0;
  19. }
  20. }
  21.  
  22. int cheiaPilha(struct Pilha *pilha){
  23. if(pilha->topo == MAX - 1){
  24. return 1;
  25. }else{
  26. return 0;
  27. }
  28. }
  29.  
  30. int tamanhoPilha(struct Pilha *pilha){
  31. return pilha->topo + 1;
  32. }
  33.  
  34. int push(struct Pilha *pilha, int valor){
  35. if(cheiaPilha(pilha)){
  36. return 0;
  37. }else{
  38. pilha->vetor[ ++(pilha->topo) ] = valor;
  39. return 1;
  40. }
  41. }
  42.  
  43. int pop(struct Pilha *pilha, int *dado){
  44. if(vaziaPilha(pilha)){
  45. return 0;
  46. }else{
  47. *dado = pilha->vetor[ pilha->topo ];
  48. --(pilha->topo);
  49. return 1;
  50. }
  51. }
  52.  
  53. int topoPilha(struct Pilha *pilha, int *dado){
  54. if(vaziaPilha(pilha)){
  55. return 0;
  56. }else{
  57. *dado = pilha->vetor[ pilha->topo ];
  58. return 1;
  59. }
  60. }
  61.  
  62. int mostrarPilha(struct Pilha *pilha){
  63.  
  64. int count, posicao;
  65.  
  66. if(vaziaPilha(pilha)){
  67. return 0;
  68. }
  69.  
  70. printf("\nTamanho da pilha: %d elemento(s).\n", pilha->topo + 1);
  71. printf("----------------------------------------------------\n");
  72. //Demostra os elementos da pilha do baixo para cima e sua posição
  73. for(count = pilha->topo + 1, posicao = 1; count > 0; count--, posicao++){
  74. printf("\n\tPosicao: %d\n", posicao);
  75. printf("\tValor: %d\n", pilha->vetor[count - 1]);
  76. }
  77. printf("\n");
  78. return 1;
  79. }
  80.  
  81. int removeValor(struct Pilha *pilha, int valor){
  82. //Pilha auxiliar
  83. struct Pilha aux;
  84. iniciaPilha(&aux);
  85.  
  86. //Variavel auxiliar para desempilhar e empilhar um elemento qualquer
  87. int i;
  88. int total = 0;
  89.  
  90. if(vaziaPilha(pilha)){
  91. return 0;
  92. }
  93.  
  94. while(vaziaPilha(pilha) != 1){
  95. //Se o elemento for diferente do valor procurado, será desempilhado e empilhado na pilha auxiliar
  96. if(pilha->vetor[ pilha->topo ] != valor){
  97. pop(pilha, &i );
  98. push(&aux, i );
  99. }else{
  100. //Se o elemento for encontrado é apenas ignorado e pulará para o proximo elemento
  101. --(pilha->topo);
  102. total++;
  103. }
  104. }
  105.  
  106. //Desempilha da auxiliar e empilha na pilha inicial todos os elementos que são diferentes do valor fornecido
  107. while(vaziaPilha(&aux)!=1){
  108. pop(&aux, &i );
  109. push(pilha, i );
  110. }
  111. //Retorna a quantidade de vezes que o valor dado foi encontrado
  112. return total;
  113. }
  114.  
  115. int comparaPilha(struct Pilha *pilha, struct Pilha *pilha_2){
  116.  
  117. int i;
  118.  
  119. //Se for diferente o tamanho das pilhas será encerrada a função retornando FALSO
  120. if(tamanhoPilha(pilha) != tamanhoPilha(pilha_2)){
  121. return 0;
  122. }else{
  123. //Compara cada elemento da pilha, se um dos elementos for diferente a função retorna FALSO
  124. for(i = pilha->topo; i >= 0; i--){
  125. if( pilha->vetor[ i ] != pilha_2->vetor[ i ]){
  126. return 0;
  127. }
  128. }
  129. return 1;
  130. }
  131. }
  132.  
  133. int invertePilha(struct Pilha *pilha){
  134. int i, aux;
  135.  
  136. if(vaziaPilha(pilha)){
  137. return 0;
  138. }
  139. //Troca o primeiro elemento da pilha com o ultimo, o segundo com o penultimo e assim por diante...
  140. for( i = 0; i <= (pilha->topo / 2) ; i++){
  141. aux = pilha->vetor[ i ];
  142. pilha->vetor[ i ] = pilha->vetor[pilha->topo - i];
  143. pilha->vetor[pilha->topo - i] = aux;
  144. }
  145.  
  146. return 1;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment