/** * This implements a circular array version of a queue */ class JollyQ3 implements Queue // save as JollyQ3.java { private int [] storage; private int front, back, length, size; static final int DEFAULT_SIZE = 1000; public JollyQ3(int m) { size = m; storage = new int[size]; front = back = 0; length = 0; } public JollyQ3() { this( DEFAULT_SIZE ); } public void enterQ( int a ) { if ( !isQFull() ) { storage[back] = a; back = (++back) % size; length++; } else System.out.println("Awfully sorry, but we're full"); } public int leaveQ() { if ( !isQEmpty() ) { int temp = storage[front]; front = (++front) % size; length--; return temp; } else { System.out.println("Awfully sorry, but we're empty"); return 0; // this is not ideal for an error condition // should really have some exception handling here } } public int peekAtQ(){ return storage[front]; } public int qLength(){ return length; } public boolean isQEmpty(){ return (length == 0); } public boolean isQFull(){ return (length == size); } public int qCapacity(){ return size; } // total capacity }