Advertisement
andresnogales

CircularQueue.java

Oct 13th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. public class  CircularQueue {
  2.     private Integer[] elements;
  3.    
  4.     private int actualSize; //Tamaño actual de la cola    
  5.     private int maxSize; //Tamaño máximo de la cola circular
  6.  
  7.     private int tail; //Apunta a la posición donde se guardará un elemento
  8.     private int head; //Apunta a la posición del elemento que saldrá
  9.  
  10.     public CircularQueue(int maxSize) {
  11.         this.maxSize = maxSize;
  12.         actualSize = 0;
  13.         head = 0;
  14.         tail = 0;
  15.         elements = new Integer[this.maxSize];                
  16.     }
  17.  
  18.     /**
  19.      * Encolar elementos
  20.      */
  21.     public void enqueue(Integer element){
  22.         if (isFull()) {
  23.             throw new RuntimeException("Error: La cola está llena");
  24.         }
  25.  
  26.         elements[tail] = element;
  27.         tail = next(tail);
  28.         actualSize++;
  29.     }
  30.    
  31.     public int next(int position) {
  32.         position++;
  33.         if(position >= maxSize) position = 0;
  34.         return position;       
  35.     }
  36.  
  37.     /**
  38.      * Desencolar elementos
  39.      */
  40.     public Integer dequeue(){
  41.        
  42.         if (isEmpty()) {
  43.             throw new RuntimeException("Error: La cola está vacía");
  44.         }
  45.        
  46.         Integer deQueuedElement = elements[head];
  47.         head = next(head);
  48.         actualSize--;
  49.         return deQueuedElement;
  50.     }
  51.  
  52.     /**
  53.      * Verifica si la cola está llena
  54.      */
  55.     public boolean isFull() {
  56.         return (actualSize == elements.length);
  57.     }
  58.  
  59.     /**
  60.      * Verifica si la cola está vacía
  61.      */
  62.     public boolean isEmpty() {
  63.         return (actualSize <= 0);
  64.     }
  65.  
  66.     public void print() {
  67.         if(isEmpty()) {
  68.             System.out.println("La cola está vacía");
  69.         }
  70.         else {
  71.             int i = 0;
  72.             int index = head;
  73.             System.out.print("Cola: ");
  74.             while(i < actualSize) {
  75.                 System.out.print(elements[index] + " ");
  76.                 index = next(index);
  77.                 i++;
  78.             }
  79.         }        
  80.     }
  81.  
  82.     public int getActualSize() {
  83.         return actualSize;
  84.     }
  85.  
  86.     public int getMaxSize() {
  87.         return maxSize;
  88.     }
  89.  
  90.     public int getTail() {
  91.         return tail;
  92.     }
  93.  
  94.     public int getHead() {
  95.         return head;
  96.     }
  97.  
  98.     public Integer[] getElements() {
  99.         return elements;
  100.     }
  101.    
  102.    
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement