Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CircularQueue {
- private Integer[] elements;
- private int actualSize; //Tamaño actual de la cola
- private int maxSize; //Tamaño máximo de la cola circular
- private int tail; //Apunta a la posición donde se guardará un elemento
- private int head; //Apunta a la posición del elemento que saldrá
- public CircularQueue(int maxSize) {
- this.maxSize = maxSize;
- actualSize = 0;
- head = 0;
- tail = 0;
- elements = new Integer[this.maxSize];
- }
- /**
- * Encolar elementos
- */
- public void enqueue(Integer element){
- if (isFull()) {
- throw new RuntimeException("Error: La cola está llena");
- }
- elements[tail] = element;
- tail = next(tail);
- actualSize++;
- }
- public int next(int position) {
- position++;
- if(position >= maxSize) position = 0;
- return position;
- }
- /**
- * Desencolar elementos
- */
- public Integer dequeue(){
- if (isEmpty()) {
- throw new RuntimeException("Error: La cola está vacía");
- }
- Integer deQueuedElement = elements[head];
- head = next(head);
- actualSize--;
- return deQueuedElement;
- }
- /**
- * Verifica si la cola está llena
- */
- public boolean isFull() {
- return (actualSize == elements.length);
- }
- /**
- * Verifica si la cola está vacía
- */
- public boolean isEmpty() {
- return (actualSize <= 0);
- }
- public void print() {
- if(isEmpty()) {
- System.out.println("La cola está vacía");
- }
- else {
- int i = 0;
- int index = head;
- System.out.print("Cola: ");
- while(i < actualSize) {
- System.out.print(elements[index] + " ");
- index = next(index);
- i++;
- }
- }
- }
- public int getActualSize() {
- return actualSize;
- }
- public int getMaxSize() {
- return maxSize;
- }
- public int getTail() {
- return tail;
- }
- public int getHead() {
- return head;
- }
- public Integer[] getElements() {
- return elements;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement