Professional Documents
Culture Documents
Array Queue PDF
Array Queue PDF
Queues;
public ArrayQueue() {
this(10);
}
@SuppressWarnings("unchecked")
public ArrayQueue(int size) {
this.items = (T[])new Object[size];
}
@Override
public void enqueue(T item) {
if (isFull())
throw new IllegalStateException();
items[rear] = item;
// Circular array
rear = (rear + 1) % items.length;
count++;
}
@Override
public T dequeue() {
if (isEmpty())
throw new IllegalStateException();
T item = items[front];
items[front] = null;
@Override
public T peek() {
if (isEmpty())
throw new IllegalStateException();
return items[front];
}
@Override
public boolean isEmpty() {
return count == 0;
}
@Override
public boolean isFull() {
return count == items.length;
}
}