jtentor icon

DemoQueue1 - Queue.java

jtentor | PRO | 05/17/20 03:26:01 PM UTC | 0 ⭐ | 822 👁️ | Never ⏰ | []
Java |

1.19 KB

|

None

|

0 👍

/

0 👎

public class Queue<ELEMENT> {
    private ELEMENT[] data;
    private int head;
    private int tail;
    private int count;
 
    public Queue() {
        this(10);
    }
 
    public Queue(int capacity) {
        this.data = (ELEMENT[]) new Object[capacity];
        this.head = 0;
        this.tail = 0;
        this.count = 0;
    }
 
    // Equivalent to Count property in C#
    public int size() {
        return this.count;
    }
 
    private int Next(int position) {
        return (++position >= this.data.length)? 0: position;
    }
 
    // Equivalent to Enqueue method in C#
    public void add(ELEMENT element) {
        if (this.count >= this.data.length) {
            throw new RuntimeException("La cola está llena...");
        }
        this.data[this.tail] = element;
        this.tail = this.Next(this.tail);
        ++this.count;
    }
 
    // Equivalent to Dequeue method in C#
    public ELEMENT remove() {
        if (this.count <= 0) {
            throw new RuntimeException("La cola está vacía...");
        }
        ELEMENT temp = this.data[this.head];
        this.head = this.Next(this.head);
        --this.count;
        return temp;
    }
}

Comments