public class QueueAsArray implements SeqStructure {

    private Object[] q; // array to hold info
    private int head;   // points to element for deQ
    private int tail;   // points to empty slot for enQ

    // Create new Queue:
    public QueueAsArray(int size) { q=new Object[size]; }
    
    // Add element to Queue:
    public void put(Object o) { enqueue(o); }
    private void enqueue(Object o) {
	if (!isFull()) {
	    q[tail]=o; 
	    tail=(tail+1)%q.length;
	} else {
	    System.out.println("Full!");
	    System.exit(0);
	}
    }

    // Take element from Queue:
    public Object get() { return dequeue(); }
    private Object dequeue() {
	Object o = null;
	if (!isEmpty()) {
	    o = q[head];
	    head=(head+1)%q.length;
	} else {
	    System.out.println("Empty!");
	    System.exit(0);
	}
	return o;
    }

    // Check if empty:
    public boolean isEmpty() { return head==tail; }
    
    // Check if full:
    public boolean isFull() { 
	return head==(tail+1) % q.length; } 


    // Not really using size here:
    public int size() { return 0;}

    // Provide Queue contents:
    public String toString() {
	if (isEmpty()) return "Empty queue";
	String rString = "";
	int current = head;
	do {
	    rString = rString + " " + q[current];
	    current++;     
	    // Check for wrap-around:
	    if (current == q.length) current = 0;
	} while (current != tail);
	return rString;
    }
    
} // Class QueueAsArray
