Advertisement
fel486

Fila.cpp

Mar 10th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include "Fila.h"
  3.  
  4. using namespace std;
  5.  
  6. struct fila
  7. {
  8.     int inicio;
  9.     int itens[TAM_MAX];
  10.     int fim;
  11. };
  12.  
  13. Fila* criar()
  14. {
  15.     Fila* f = (Fila*)malloc(sizeof(Fila));
  16.     f->inicio = 0;
  17.     f->fim = 0;
  18.  
  19.     return f;
  20. }
  21.  
  22. void destruir(Fila* f)
  23. {
  24.     if (f != NULL) free(f);
  25. }
  26.  
  27. void enfileirar(Fila* f, int valor)
  28. {
  29.     if (f != NULL)
  30.     {
  31.         if (tamanho(f) == TAM_MAX)
  32.         {
  33.             cout << "Fila lotada!\n";
  34.             return;
  35.         }
  36.  
  37.         if (f->fim == TAM_MAX)
  38.         {
  39.             int a;
  40.  
  41.             for (a = 0; a < tamanho(f); a++)
  42.             {
  43.                 f->itens[a] = f->itens[f->inicio + 1];
  44.             }
  45.  
  46.             f->inicio = 0;
  47.             f->fim = a;
  48.  
  49.             cout << "Inicio: " << f->inicio << " | Fim: " << f->fim << "\n";
  50.         }
  51.  
  52.         f->itens[f->fim++] = valor;
  53.     }
  54. }
  55.  
  56. int desenfileirar(Fila* f)
  57. {
  58.     if (f != NULL)
  59.     {
  60.         if (tamanho(f) == 0)
  61.         {
  62.             cout << "Fila vazia!\n";
  63.             return -1;
  64.         }
  65.  
  66.         return f->itens[f->inicio++];
  67.     }
  68. }
  69.  
  70. int primeiro(Fila* f)
  71. {
  72.     if (f != NULL)
  73.     {
  74.         if (tamanho(f) == 0)
  75.         {
  76.             cout << "Fila vazia!\n";
  77.             return -1;
  78.         }
  79.  
  80.     }
  81.  
  82.     return f->itens[f->inicio];
  83. }
  84.  
  85. int tamanho(Fila* f)
  86. {
  87.     if (f != NULL) return (f->fim) - (f->inicio);
  88.     return -1;
  89. }
  90.  
  91. void exibirFila(Fila* f)
  92. {
  93.     cout << "Status da fila: [ ";
  94.  
  95.     if(tamanho(f) == 0)
  96.     {
  97.         cout << "Fila vazia! ]\n";
  98.             return;
  99.     }
  100.  
  101.     for (int a = f->inicio; a < f->fim; a++)
  102.     {
  103.         cout << f->itens[a] << " ";
  104.     }
  105.  
  106.     cout << " ]\n";
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement