Advertisement
Guest User

cod

a guest
May 3rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Fila.h"
  4.  
  5. struct No * inserir(struct No *q, int num){
  6. struct No * anterior = q;
  7. struct No *novo = (struct No*)malloc(sizeof(struct No));
  8. if(novo == NULL){
  9. printf("Erro de alocacao\n");
  10. return q;
  11. }
  12. novo->num = num;
  13. if(q==NULL){
  14. novo->prox = NULL;
  15. q = novo;
  16. return q;
  17. }
  18. while(anterior->prox != NULL)
  19. anterior = anterior->prox;
  20. novo->prox = NULL;
  21. anterior->prox = novo;
  22. return q;
  23. }
  24. struct No *remover(struct No *q, int *num){
  25. struct No * tmp = q;
  26. if(q == NULL){
  27. printf("Fila vazia\n");
  28. return q;
  29. }
  30. *num = q->num;
  31. if(tmp->prox == NULL){
  32. free(tmp);
  33. q = NULL;
  34. return q;
  35. }
  36. tmp = q;
  37. q = q->prox;
  38. free(tmp);
  39. return q;
  40. }
  41. int tamanho(struct No *q){
  42. struct No *anterior = q;
  43. int cont = 1;
  44. if(q==NULL){
  45. printf("Fila vazia\n");
  46. return 0;
  47. }
  48. while(anterior->prox != NULL){
  49. cont++;
  50. anterior = anterior->prox;
  51. }
  52. return cont;
  53. }
  54. void imprimir(struct No *q){
  55. struct No *anterior = q;
  56. if(q==NULL){
  57. printf("Fila vazia\n");
  58. return;
  59. }
  60. printf("%d ",anterior->num);
  61. while(anterior->prox != NULL){
  62. printf("-->%d ",anterior->prox->num);
  63. anterior = anterior->prox;
  64. }
  65. printf("\n");
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement