class Queue {
class QueueNode {
int val;
QueueNode next;
public QueueNode(int val) {
this.val = val;
}
}
QueueNode first, last;
void add(int val) {
QueueNode node = new QueueNode(val);
if (last != null) last.next = node;
last = node;
if (first == null) first = last;
}
int remove() {
if (first == null) throw new EmptyQueueException();
int val = first.val;
first = first.next;
if (first == null) last = null;
return val;
}
int peek() {
if (first == null) throw new EmptyQueueException();
return first.val;
}
boolean isEmpty() {
return first == null;
}
}