Advertisement
cesarnascimento

fila

Mar 23rd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package exercicios.fila.vetor;
  2.  
  3. public class Fila {
  4.  
  5.     private int front = 0;
  6.     private int back = 0;
  7.     private int array[] = new int[2];
  8.  
  9.     public void enqueue(int i) {
  10.         if (back == 0) {
  11.             array[back] = i;
  12.             back++;
  13.             System.out.println(i + " foi adcionado");
  14.         } else if (back == front) {
  15.             System.out.println("Fila cheia!");
  16.         } else if (back == array.length) {
  17.             back = 0;
  18.         } else {
  19.             array[back] = i;
  20.             back++;
  21.             System.out.println(i + " foi adcionado");
  22.         }
  23.     }
  24.  
  25.     public int dequeue() {
  26.         if (front > back) {
  27.             System.out.println("lista vazia");
  28.         } else if (front == array.length) {
  29.             front = 0;
  30.         } else {
  31.             front++;
  32.         }
  33.         return 0;
  34. }
  35. }
  36.  
  37.  
  38. package exercicios.fila.vetor;
  39.  
  40. public class Node {
  41.  
  42.     public int info;
  43.     Node next=null;
  44.     Node previous= null;
  45.    
  46.     public Node(int i) {
  47.         this.info = i;
  48.     }
  49.    
  50.     public int getNode() {
  51.         return info;
  52.     }
  53.     public void setNode(int node) {
  54.         this.info = node;
  55.     }
  56.     public Node getNext() {
  57.         return next;
  58.     }
  59.     public void setNext(Node next) {
  60.         this.next = next;
  61.     }
  62.     public Node getPrevious() {
  63.         return previous;
  64.     }
  65.     public void setPrevious(Node previous) {
  66.         this.previous = previous;
  67.     }
  68. }
  69.  
  70.  
  71. package exercicios.fila.vetor;
  72.  
  73. public class TestFila {
  74.  
  75.     public static void main(String[] args) {
  76.  
  77.         Fila f = new Fila();
  78.        
  79.         f.enqueue(1);
  80.         f.enqueue(2);
  81.        
  82.         f.dequeue();
  83.        
  84.         f.enqueue(5);
  85.  
  86.  
  87.     }
  88.  
  89. }
  90.  
  91. package exercicios.fila.vetor.node;
  92.  
  93. import exercicios.fila.vetor.Node;
  94.  
  95. public class Fila {
  96.    
  97.     private Node front=null;
  98.     private Node back=null;
  99.    
  100.     public void enqueue(Node i) {
  101.         if (back == null) {
  102.             front = back = i;
  103.         } else {
  104.             back.setNext(i);
  105.             i.setPrevious(back);
  106.             back = i;
  107.         }
  108.         System.out.println(back.info);
  109.     }
  110.    
  111.     public void dequeue() {
  112.         if (front == null && back == null) {
  113.             System.out.println("Fila vazia!");
  114.         } else {
  115.             front = front.getNext();
  116.             front.setPrevious(null);
  117.         }
  118.        
  119.         System.out.println(front.info);
  120.     }
  121. }
  122.  
  123.  
  124. package exercicios.fila.vetor.node;
  125.  
  126. import exercicios.fila.vetor.Node;
  127.  
  128. public class TesteFila {
  129.  
  130.     public static void main(String[] args) {
  131.        
  132.         Fila f = new Fila();
  133.        
  134.         f.enqueue(new Node(5));
  135.         f.enqueue(new Node(3));
  136.        
  137.         f.dequeue();
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement