package rebelsky.linear; /** * An implementation of queues using linked nodes. * * @author Samuel A. Rebelsky * @version 1.0 of October 2004. */ public class LinkedQueue implements Queue { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The "last" thing. */ QueueNode back; /** The "first" thing. */ QueueNode front; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public LinkedQueue() { this.front = null; this.back = null; } // LinkedQueue() // +---------+------------------------------------------------- // | Methods | // +---------+ public void put(Object addMe) { // Build a new QueueNode QueueNode newEnd = new QueueNode(addMe, null); // Sanity check! Is the list empty? if (this.back == null) { this.back = newEnd; this.front = newEnd; } else { // Make the back QueueNode refer to that as "rest" // The current back's next element should be newEnd this.back.next = newEnd; // * Update back this.back = this.back.next; } } // put(Object) public Object get() { // Get the value stored in the front QueueNode Object value = this.front.data; // Move front to the next QueueNode this.front = this.front.next; // Sanity check! Did we just make the queue // empty? if (this.front == null) this.back = null; // That's it return value; } // get() public Object peek() { return this.front.data; } // peek() public boolean isEmpty() { return this.back == null; } // isEmpty() } // class LinkedQueue