【发布时间】:2017-02-10 06:35:37
【问题描述】:
我对 C++ 中的链表基本上没有任何问题,但出于某种原因,这对我来说很重要。我使用提供的包中的其他类打印了一个节点,但随着我的继续,我一直在撞墙。
下面的代码是乱七八糟的,因为我在修补。我只是不知道从这里去哪里。到目前为止,这是一个空指针异常。
仅供参考:poll() 只是移除当前头部并返回它,offer() 正在添加到后面。到目前为止,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”,那么您更有可能得到答案,而不是“事情不正常,我希望您能帮助我”。 -
真的,对不起。到目前为止,我从“ oldLast.next = last;”中得到了例外在报价功能中。
-
如果您从那里获得
NPE,那么您的oldLast为空。无论何时收到NullPointerException,请参阅stackoverflow.com/questions/218384/…。 -
会的。谢谢您的帮助!不过,我会假设代码存在更多问题。
标签: java linked-list queue fifo