我在C++中基本上没有链接列表的问题,但是出于某种原因,这会让我感到困惑。我使用提供的包中的其他类打印出单个节点,但是随着我继续往下走,我只是一直跑到墙上。如何调试我的Java链接列表队列?
下面的代码是由于我的修补而引起的混乱。我不知道该从哪里出发。截至目前,这是获得空指针例外。
仅供参考:poll()
只是删除当前的头部并返回它,offer()
正在添加到后方。截至目前,报价方法中的例外情况为oldLast.next = last
。
我不是要求任何人完全解决这个问题。我只是需要一些提示来进步。
public class FIFOQueue implements Queue {
//put your name as the value of the signature.
String signature = "name";
Node head = new Node(null);
Node pointer = head;
Node first;
Node last;
Node prev;
Node curr;
class Node {
Process process;
Node next;
Node(Process p) {
this.process = p;
this.next = null;
}
}
@Override
public void offer(Process p) {
if(head == null)
{
head = new Node(p);
first = head;
last = head;
}
else
{
Node oldLast = last;
Node newNode = new Node(p);
last = newNode;
oldLast.next = last;
}
}
@Override
public Process poll() {
if(isEmpty())
throw new NoSuchElementException();
Node oldPointer = first;
first = first.next;
head = first;
return oldPointer.process;
}
@Override
public boolean isEmpty() {
return head == null;
}
@Override
public String getSignature() {
return signature;
}
}
你需要更具体。如果你说“我在第15行有一个ArrayIndexOutOfBoundsException”而不是“事情不能工作,我希望你能帮助我”,那么你更有可能得到答案。 – Kayaman
真的够了,对不起。截至目前,我正在从“\t \t \t oldLast.next = last;”中得到例外。在报价功能中。 – Clannadqs
如果你从那里得到一个'NPE',那么你的'oldLast'就是null。无论何时出现'NullPointerException',请参阅http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it。 – Kayaman