ArrayQueue.java

public class ArrayQueue implements Queue {
    private Object[] array;
    private int length=0;
    private int maxLength;
    private int head=0, tail = -1;

    public ArrayQueue(int maxLength) {
	this.maxLength = maxLength;
	array = new Object[maxLength];
    }

    public void insert(Object o) {
	tail = (tail + 1) % maxLength;
	array[tail] = o;
	length++;
    }

    public Object remove() {

	//Can't remove anything if there is nothing to remove (length==0)
	if (length>0) {
	    Object object=array[head]; //hold the first object temporarily
	    length--;
	    head = (head + 1) % maxLength;
	    return object;
	} else {
	    System.out.println("ERROR: Can't remove object when Queue is empty.");
	    return null;
	}
    }

    public int getLength(){
	return array.length;
    }

}



Generated by GNU enscript 1.6.3.