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 } // this is a linear array implementation 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 } // this implements a circular array version of a queue +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public class QThingy // save as QThingy.java { private int value; private QThingy next; public QThingy(int value, QThingy next) { setValue(value); setQThingy(next); } public QThingy(int value) { this(value, null); } public QThingy() { this(0); } public void setValue(int value) { this.value = value; } public int getValue() { return this.value; } public void setNext(Qthingy next) { this.next = next; } public QThingy getNext() { return this.next; } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class JollyQ4 implements Queue { private QThingy header, front, back; private int length; static final int DEFAULT_SIZE = 0; public JollyQ4(int m) // this int m is quite vacuous!!! { header = new QThingy(); back = header; length = 0; } public JollyQ4() { this( DEFAULT_SIZE ); } public void enterQ( int a ) { QThingy tempQThingy = new QThingy( a ); back.setNext(tempQThingy); back = tempQThingy; if ( isQEmpty() ) front = back; length++; } public int leaveQ() { if (!isQEmpty() ) { int tempValue = front.getValue(); front = front.getNext(); length--; return tempValue; } 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() { if ( !isQEmpty() ) return front.getValue(); return 0; // this is not ideal for an error condition // should really have some exception handling here} public int qLength(){ return length; } public boolean isQEmpty(){ return (length == 0); } public boolean isQFull(){ return false; } public int qCapacity(){ return Integer.MAX_VALUE; } // total capacity } // this implements a non-array version of a queue +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++