/**
* This implements a non-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
}