class AQueue<E> implements Queue<E> {
private E queueArray[];
private static final int DEFAULT_SIZE = 10;
private int maxSize;
private int front;
private int rear;
@SuppressWarnings("unchecked")
AQueue(int size) {
maxSize = size+1;
rear = 0; front = 1;
queueArray = (E[])new Object[maxSize];
}
AQueue() {
this(DEFAULT_SIZE);
}
public void clear() {
rear = 0; front = 1;
}
public boolean enqueue(E it) {
if (((rear+2) % maxSize) == front) {
return false;
}
rear = (rear+1) % maxSize;
queueArray[rear] = it;
return true;
}
public E dequeue() {
if(length() == 0) {
return null;
}
E it = queueArray[front];
front = (front+1) % maxSize;
return it;
}
public E frontValue() {
if (length() == 0) {
return null;
}
return queueArray[front];
}
public int length() {
return ((rear+maxSize) - front + 1) % maxSize;
}
public boolean isEmpty() {
return front - rear == 1;
}
}