/* * File: ZHLinkedQueue.java */ package zhstructures; import java.util.Iterator; import java.util.NoSuchElementException; /** * Class implementing a ZHQueue using a one-way linked structure. * * @author J. Andrew Holey * @version August 11, 2008 */ public class ZHLinkedQueue implements ZHQueue { /** * A reference to the first node in this queue. */ protected QueueNode front; /** * A reference to the empty node at the back of this queue. */ protected QueueNode rear; /** * Creates a new empty queue. */ public ZHLinkedQueue() { this.front = new QueueNode(); this.rear = this.front; } /* (non-Javadoc) * @see zhstructures.ZHQueue#getFirst() */ public ElementType peek() { if (this.isEmpty()) throw new NoSuchElementException(); return this.front.element; } /* (non-Javadoc) * @see zhstructures.ZHQueue#dequeue() */ public ElementType dequeue() { if (this.isEmpty()) throw new NoSuchElementException(); ElementType result = this.front.element; this.front = this.front.next; return result; } /* (non-Javadoc) * @see zhstructures.ZHStack#push(java.lang.Object) */ public void enqueue(ElementType element) { this.rear.element = element; this.rear.next = new QueueNode(); this.rear.state = ZHComponentState.NOT_EMPTY; this.rear = this.rear.next; } /* (non-Javadoc) * @see zhstructures.ZHCollection#contains(java.lang.Object) */ public boolean contains(ElementType element) { return this.front.contains(element); } /* (non-Javadoc) * @see zhstructures.ZHCollection#isEmpty() */ public boolean isEmpty() { return this.front.isEmpty(); } /* (non-Javadoc) * @see zhstructures.ZHCollection#iterator() */ public Iterator iterator() { return this.front.iterator(); } /** * Class implementing nodes for this queue. */ protected class QueueNode extends ZHOneWayLinkedStructure { /** * Creates a new empty queue node. */ protected QueueNode() { super(); } } }