public class LinkedQueue
private static class Elem
private T value;
private Elem
private Elem(T value, Elem
this.value = value;
this.next = next;
}
}
private Elem
private Elem
public void enqueue(E value) {
if (value == null) {
throw new NullPointerException();
}
Elem
newElem = new Elem
if (rear == null) {
front = rear = newElem;
} else {
rear.next = newElem;
rear = newElem;
}
}
public E peek() {
if (front == null) {
throw new EmptyQueueException();
}
return front.value;
}
public E dequeue() {
if (front == null) {
throw new EmptyQueueException();
}
E result = front.value;
if (front.next == null) {
front = rear = null;
} else {
front = front.next;
}
return result;
}
public boolean isEmpty() {
return front == null;
}
@Override
public String toString() {
String str = “[“;
Elem
while (p != null) {
if (p != front) {
str += “, “;
}
str += p.value;
p = p.next;
}
str += “]”;
return str;
}
}