Advertisement
Herowaree

Fila em Java

Nov 25th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public class Fila {
  2. private static final int MAX = 100;
  3. private int tamanho;
  4. private int inicio;
  5. private int fim;
  6. private int vetor[] = new int [MAX];
  7.  
  8. public Fila(){
  9. this.tamanho = 0;
  10. this.inicio = 0;
  11. this.fim = -1;
  12. }
  13.  
  14. public int getTamanho(){
  15. return this.tamanho;
  16. }
  17.  
  18. public boolean vaziaFila(){
  19. return this.tamanho == 0;
  20. }
  21.  
  22. public boolean cheiaFila(){
  23. return this.tamanho == MAX;
  24. }
  25.  
  26. public void insereFila(int valor){
  27.  
  28. if(this.cheiaFila()){
  29. System.out.println("Fila cheia.");
  30. }
  31.  
  32. this.fim = (this.fim + 1) % MAX;
  33. this.vetor[ this.fim ] = valor;
  34. this.tamanho++;
  35. }
  36.  
  37. public int removeFila(){
  38. if(this.vaziaFila()){
  39. System.out.println("Fila vazia.");
  40. return 0;
  41. }
  42. int i = this.vetor[ this.inicio ];
  43. this.inicio = (this.inicio + 1) % MAX;
  44. this.tamanho--;
  45.  
  46. return i;
  47. }
  48.  
  49. public int primeiroFila(){
  50. if(this.vaziaFila()){
  51. return 0;
  52. }else{
  53. return this.vetor[ this.inicio ];
  54. }
  55. }
  56.  
  57. public void mostraFila(){
  58. if(this.vaziaFila()){
  59. System.out.println("Fila vazia.");
  60. }
  61.  
  62. int posicao = 1;
  63. for(int i = this.inicio; i != ( (this.fim + 1) % MAX ); i++, posicao++){
  64. System.out.println("Posição: "+ posicao +" / Valor: " + vetor[i]);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement