Advertisement
Herowaree

Fila Encadeada.c

Nov 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct No{
  5. int dado;
  6. struct No *proximo;
  7. };
  8.  
  9. struct Fila{
  10. int tamanho;
  11. struct No *inicio;
  12. struct No *fim;
  13. };
  14.  
  15. int iniciaFila(struct Fila *fila){
  16. fila->tamanho = 0;
  17. fila->inicio = NULL;
  18. fila->inicio = NULL;
  19. }
  20.  
  21. void vaziaFila(struct Fila *fila){
  22. if(fila->tamanho == 0){
  23. return 1;
  24. }else{
  25. return 0;
  26. }
  27. }
  28.  
  29. int tamanhoFila(struct Fila *fila){
  30. return fila->tamanho;
  31. }
  32.  
  33. int primeiroFila(struct Fila *fila, int *dado){
  34. if(vaziaFila(fila)) return 0;
  35.  
  36. *dado = (fila->inicio)->dado;
  37.  
  38. return 1;
  39. }
  40.  
  41. int insereFila(struct Fila *fila, int valor){
  42. struct No *novoNo;
  43. novoNo = malloc(sizeof(struct No));
  44.  
  45. if(novoNo == NULL) return 0;
  46.  
  47. novoNo->dado = valor;
  48. novoNo->proximo = NULL;
  49.  
  50. if(vaziaFila(fila)){
  51. fila->inicio = novoNo;
  52. fila->fim = novoNo;
  53. }else{
  54. (fila->fim)->proximo = novoNo;
  55. fila->fim = novoNo;
  56. }
  57.  
  58. fila->tamanho++;
  59.  
  60. return 1;
  61. }
  62.  
  63. int removeFila(struct Fila *fila, int *dado){
  64. struct No *aux;
  65.  
  66. if(vaziaFila(fila)) return 0;
  67.  
  68. if(fila->inicio == fila->fim){
  69. fila->fim = NULL;
  70. }
  71.  
  72. *dado = (fila->inicio)->dado;
  73. aux = fila->inicio;
  74. fila->inicio = aux->proximo;
  75. free(aux);
  76.  
  77. fila->tamanho--;
  78.  
  79. return 1;
  80. }
  81.  
  82. int mostraFila(struct Fila *fila){
  83. struct No *aux = fila->inicio;
  84.  
  85. if(vaziaFila(fila)) return 0;
  86.  
  87. printf("Tamanho da fila: %d elemento(s)\n", fila->tamanho);
  88. printf("----------------------------------\n");
  89. int indice = 1;
  90. while(aux != NULL){
  91. printf("\tPOSICAO: %d\n", indice);
  92. printf("\tVALOR: %d\n", aux->dado);
  93. aux = aux->proximo;
  94. indice++;
  95. printf("\n");
  96. }
  97. return 1;
  98. }
  99.  
  100. int procuraFila(struct Fila *fila, int valor){
  101.  
  102. if(vaziaFila(fila)) return -1;
  103. struct No *aux = fila->inicio;
  104.  
  105. int posicao = 1;
  106. while(aux != NULL){
  107. if(aux->dado == valor){
  108. return posicao;
  109. }
  110. posicao++;
  111. aux = aux->proximo;
  112. }
  113.  
  114. return -1;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement