/** * This is a linear array implementation of a queue */ class JollyQ2 implements Queue // save as JollyQ2.java { private int [] storage; private int front, back, length, size; static final int DEFAULT_SIZE = 1000; public JollyQ2(int m) { size = m; storage = new int[size]; front = back = 0; length = 0; } public JollyQ2() { this( DEFAULT_SIZE ); } public void enterQ( int a ) { if ( !isQFull() ) {storage[back] = a; back++; length++;} else System.out.println("Awfully sorry, but we're full"); } public int leaveQ() { if (!isQEmpty() ) { int temp = storage[front]; front++; length--; return temp; } else { System.out.println("Awfully sorry, but we're empty"); return 0.0; // this is not ideal, should handle this error state better } } 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 }