Herowaree

PILHA_ENC.c / Char

Nov 29th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct No{
  5. char dado;
  6. struct No *proximo;
  7. };
  8.  
  9. struct Pilha{
  10. int tamanho;
  11. struct No *cabeca;
  12. };
  13.  
  14. void iniciaPilha(struct Pilha *pilha){
  15. pilha->tamanho = 0;
  16. pilha->cabeca = NULL;
  17. }
  18.  
  19. int tamanhoPilha(struct Pilha *pilha){
  20. return pilha->tamanho;
  21. }
  22.  
  23. int vaziaPilha(struct Pilha *pilha){
  24. if(pilha->tamanho == 0){
  25. return 1;
  26. }else{
  27. return 0;
  28. }
  29. }
  30.  
  31. char topoPilha(struct Pilha *pilha){
  32. if(vaziaPilha(pilha)) return '0';
  33.  
  34. return (pilha->cabeca)->dado;
  35. }
  36.  
  37. int push(struct Pilha *pilha, char caracter){
  38. struct No *novoNo;
  39. novoNo = malloc(sizeof(struct No));
  40.  
  41. if(novoNo == NULL) return 0;
  42.  
  43. novoNo->dado = caracter;
  44. novoNo->proximo = pilha->cabeca;
  45. pilha->cabeca = novoNo;
  46.  
  47. pilha->tamanho++;
  48.  
  49. return 1;
  50. }
  51.  
  52. int pop(struct Pilha *pilha, char *caracter){
  53. struct No *aux = pilha->cabeca;
  54.  
  55. if(vaziaPilha(pilha)) return 0;
  56.  
  57. *caracter = (pilha->cabeca)->dado;
  58. pilha->cabeca = aux->proximo;
  59. free(aux);
  60. pilha->tamanho--;
  61.  
  62. return 1;
  63. }
  64.  
  65. int mostrarPilha(struct Pilha *pilha){
  66.  
  67. struct No *aux = pilha->cabeca;
  68. int i;
  69.  
  70. if(vaziaPilha(pilha)){
  71. return 0;
  72. }
  73.  
  74. printf("Tamanho da pilha: %d elemento(s)\n", pilha->tamanho);
  75. printf("----------------------------------\n");
  76. for(i = 0; i < pilha->tamanho; i++){
  77. printf("\tPOSICAO: %d\n", i + 1);
  78. printf("\tVALOR: %c\n", aux->dado);
  79. aux = aux->proximo;
  80. printf("\n");
  81. }
  82.  
  83. return 1;
  84. }
  85.  
  86. int comparaPilha(struct Pilha *pilha, struct Pilha *pilha_2){
  87.  
  88. struct No *aux = pilha->cabeca;
  89. struct No *aux_2 = pilha_2->cabeca;
  90.  
  91. if(pilha->tamanho != pilha_2->tamanho){
  92. return 0;
  93. }
  94.  
  95. while(aux != NULL){
  96. if(aux->dado != aux_2->dado){
  97. return 0;
  98. }else{
  99. aux = aux->proximo;
  100. aux_2 = aux_2->proximo;
  101. }
  102. }
  103.  
  104. return 1;
  105. }
  106.  
  107. int invertePilha(struct Pilha *pilha){
  108. struct Pilha aux;
  109. iniciaPilha(&aux);
  110.  
  111. if(vaziaPilha(pilha)) return 0;
  112.  
  113. char i;
  114. //Passa todos os elementos da pilha principal para a auxiliar, invertendo a pilha
  115. while(vaziaPilha(pilha) != 1){
  116. pop(pilha, &i);
  117. push(&aux, i);
  118. }
  119.  
  120. //A pilha principal passa a ser a pilha auxiliar
  121. pilha->cabeca = aux.cabeca;
  122. pilha->tamanho = aux.tamanho;
  123.  
  124. return 1;
  125. }
  126.  
  127. void destruirPilha(struct Pilha *pilha){
  128. char i;
  129. while(vaziaPilha(pilha)){
  130. pop(pilha, &i);
  131. }
  132. }
  133.  
  134. int removeValor(struct Pilha *pilha, char caracter){
  135. struct Pilha aux;
  136. iniciaPilha(&aux);
  137. char i;
  138.  
  139. if(vaziaPilha(pilha)) return 0;
  140.  
  141. while(vaziaPilha(pilha) != 1){
  142. //Se o valor for igual ao fornecido, ele é apenas descartado
  143. if(topoPilha(pilha) == caracter){
  144. pop(pilha, &i);
  145. //Senão, é salvo na pilha auxiliar
  146. }else{
  147. pop(pilha, &i);
  148. push(&aux, i);
  149. }
  150. }
  151. //Insere os valores de volta a pilha original
  152. while(vaziaPilha(&aux) != 1){
  153. pop(&aux, &i);
  154. push(pilha, i);
  155. }
  156.  
  157. return 1;
  158. }
  159.  
  160. int main(){
  161.  
  162. struct Pilha pilha;
  163. iniciaPilha(&pilha);
  164.  
  165. push(&pilha, 'A');
  166. push(&pilha, 'B');
  167.  
  168. char caracter;
  169.  
  170. pop(&pilha, &caracter);
  171.  
  172. printf("%c\n", caracter);
  173. mostrarPilha(&pilha);
  174.  
  175. return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment