@SuppressWarnings("unchecked") public class LLQueue { private class Node // implementation of the Node used in the Linked List, also generic with T being the type - note, most of the methods are not needed for the queue but { // would be used in a normal linked list private T data; // the datum being stored private Node next; // pointer to the next node in the queue public Node(T d, Node n) // construct the Node given the datum and next pointer which should be null since this will be the last node in the queue { data=d; next=n; } public Node(T d) // construct the Node given the datum, make next pointer null since we don't know what the next pointer will be { data=d; next=null; } public T getData() // data accessor { return data; } public Node getNext() // next field accessor { return next; } public void setData(T d) // change the datum { data=d; } public void setNext(Node n) // change the next pointer { next=n; } } private Node front, rear; // the LL Queue has two pointers, one at the front to dequeue and one at the rear to enqueue public LLQueue() // start with an empty queue { front=null; rear=null; } public void enqueue(T data) { if(front==null) // special case, adding to an empty queue requires manipulating both front and rear { front=new Node(data,null); rear=front; } else // add new node at the end { Node newNode=new Node(data,null); // construct the new Node which will be at the end of the queue, so its next field is null rear.setNext(newNode); // have the last node point at this new node rear=newNode; // have rear now point at the new node } } public T dequeue() throws QueueEmptyException { if(front==null) throw new QueueEmptyException("Cannot dequeue, queue is empty"); // if queue is empty, throw an exception else { T temp=front.getData(); // otherwise return the front node's data and reset front front=front.getNext(); // to point at the next node if(front==null) rear=null; // reset rear pointer if the queue is now empty return temp; } } public T peek() throws QueueEmptyException { if(front==null) throw new QueueEmptyException("Cannot peek, queue is empty"); // if queue is empty, throw an exception else return front.getData(); // otherwise return the front node's data } public boolean isEmpty() { return front==null; } }