我一直在尝试在 Java 中创建一个双端队列,但是当我添加一个新节点时出现空指针异常。这是我用于双向链表的辅助类:
private class Node {
private Item item;
private Node next;
private Node prev;
}
然后我将节点添加到列表的开头,如下所示:
public void addFirst(Item item) {
Node oldfirst = first;
first = new Node();
first.prev = null;
first.item = item;
first.next = oldfirst;
oldfirst.prev = first;
N++;
}
我得到了一条NullPointer
例外oldfirst.prev = first;
。此外,当我编译代码时,我收到以下警告:
Warning: The field Deque2<Item>.Node.prev is never read locally
我可能做错了什么?