Once more, I bring you the
Java implementation of this
Data Structure. Note: this Queue takes
objects, not
primitive types. This actually makes things much easier, even though I wouldn't admit it when I first started working with Queues (or other structures for that matter). Note that this Queue is based on a Doubly
Linked List, which I have also noded. Enjoy!
//************************************************************
// This class defines a Queue.
// It is based on a Linked List.
//************************************************************
public class MyQueue extends ListClass
{
//************************************************************
// Declares an integer to hold the number of elements on the
// Queue.
//************************************************************
private int numOnQueue = 0;
//************************************************************
// This is the default constructor for the MyQueue class.
// It assigns all omnipresent nodes to null.
//************************************************************
public MyQueue()
{
start = current = end = null;
}
//************************************************************
// This method, dequeue() removes the first elements from the
// queue, and returns it to the calling object. It also
// decrements numOnQueue.
//************************************************************
public Object dequeue()
{
Object o = null;
current = start;
if(this.isEmpty() == false)
{
if(start.getNext() == null)
{
o = start.getObject();
current = start = end = null;
numOnQueue--;
return(o);
}
else
{
o = start.getObject();
start = current = start.getNext();
numOnQueue--;
return(o);
}
}
return("If you see this, there is an error.");
}
//************************************************************
// This method, enqueue() takes an object and adds it to the
// end of the queue. It also increments numOnQueue.
//************************************************************
public void enqueue(Object o)
{
addAfter(o);
numOnQueue++;
}
//************************************************************
// This method, getNumOnQueue() returns the current int value
// of numOnQueue.
//************************************************************
public int getNumOnQueue()
{
return numOnQueue;
}
//************************************************************
// This method, isEmpty() determines if the queue is empty,
// using the value of numOnQueue as an indicator.
//************************************************************
public boolean isEmpty()
{
if(numOnQueue == 0)
return true;
else
return false;
}
}