CS计算机代考程序代写 public class LinkedQueue implements Queue {

public class LinkedQueue implements Queue {

private static class Elem {

private T value;
private Elem next;

private Elem(T value, Elem next) {
this.value = value;
this.next = next;
}
}

private Elem front;
private Elem rear;

public void enqueue(E value) {

if (value == null) {
throw new NullPointerException();
}

Elem newElem;
newElem = new Elem(value, null );

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 p = front;

while (p != null) {
if (p != front) {
str += “, “;
}
str += p.value;
p = p.next;
}
str += “]”;
return str;
}

}