思路
由于链表只在头节点处增删都为O(1),那么对于队列操作,采用上一篇的链表结构,就不能做到入队和出队操作都为O(1),假设在链表头处入队,那么链表尾出队就为O(n)了,反之亦然。为了解决这个问题,我们需要在将链表尾也标记出来,记为tail。
图中可以看出,在这个实现中,由于只在头和尾进行操作,不涉及操作的统一性,因此去掉了dummyHead。
实现
public class LinkedListQueue<E> implements Queue<E> {
private Node head, tail;
private int size;
private class Node {
E e;
Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void enqueue(E e) {
if (tail == null) {
tail = new Node(e);
head = tail;
} else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
}
@Override
public E dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Cannot dequeue in a empty queue.");
}
Node delNode = head;
head = head.next;
delNode.next = null;
if (head == null) {
tail = null;
}
size--;
return delNode.e;
}
@Override
public E getFront() {
if (isEmpty()) {
throw new IllegalStateException("Cannot read in a empty queue.");
}
return head.e;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("LinkedListQueue: front ");
Node curNode = head;
while (curNode != null) {
res.append(curNode.e).append(" -> ");
curNode = curNode.next;
}
res.append("tail NULL");
return res.toString();
}
public static void main(String[] args) {
LinkedListQueue<Integer> queue = new LinkedListQueue<>();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if ((i + 1) % 3 == 0) {
queue.dequeue();
System.out.println(queue);
}
}
}
}