import ListNode; public class Queue { private ListNode front; // first node in list private ListNode tail; // last node in list private int length = 0; // queue length /** * enqueue * * insert a new node into the tail if the list */ public void enqueue( Object element ) { ListNode node = new ListNode(); node.setElement( element ); if ( isEmpty() ) { front = node; tail = node; } else { tail.setNext( node ); tail = node; } length++; } /** * dequeue * * remove and return the item at the front of the list */ public Object dequeue() { if ( isEmpty() ) { return null; } else { length--; ListNode frontNode = front; front = front.getNext(); return frontNode.getElement(); } } /** * remove * * remove current node from list */ public void remove( ListNode current ) { if ( current == front ) { front = current.getNext(); } else { // find the node before current ListNode node = front; while( current != node.getNext() ) { node = node.getNext(); } node.setNext( current.getNext() ); if ( current == tail ) { tail = node; } } length--; } /** * isEmpty * * return true if list is empty */ public boolean isEmpty() { return front == null; } /** * notEmpty * * return true if list is not empty */ public boolean notEmpty() { return front != null; } /** * getFront * * return the front node */ public ListNode getFront() { return front; } /** * setFront * * place node at front of the list */ private void setFront( ListNode n ) { if ( isEmpty() ) { enqueue( n ); } else { n.setNext( front ); front = n; } } /** * enqueueFront * * place node at front the list */ public void enqueueFront( Object element ) { ListNode n = new ListNode(); n.setElement( element ); setFront( n ); } /** * getTail * * return the last node in the list */ public ListNode getTail() { return tail; } /** * getFrontElement * * return the reference to the object in the first node */ public Object getFrontElement() { return( front.getElement() ); } /** * getQueueLength */ int getQueueLength() { return length; } }