如何调试我的 Java 链表队列?
How to debug my Java linked-list queue?
我对 C++ 中的链表基本上没有任何问题,但由于某种原因,我遇到了这个问题。我使用提供的包中的另一个 类 打印了一个节点,但随着我继续,我只是将 运行 保留在墙上。
下面的代码由于我的修改而变得混乱。我只是不知道从这里去哪里。截至目前,这是一个空指针异常。
仅供参考: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;
}
}
我认为你的核心问题在这里:
Node prev;
Node curr;
这些让你很困惑。删除它们。
Node prev;
- 这应该在 Node
class.
Node curr;
- 这应该是局部变量,而不是实例变量。
还有
Node head = new Node(null);
不与
胶凝
if(head == null)
{
head = new Node(p);
使 head == null
表示列表为空或其他 - 但要保持一致。
(代表OP发表).
public void offer(Process p) {
if(head.process == null)
{
head = new Node(p);
first = head;
last = head;
}
last.next = new Node(p);
last = last.next;
}
这解决了我的问题。不敢相信我让这让我感到困惑。
我对 C++ 中的链表基本上没有任何问题,但由于某种原因,我遇到了这个问题。我使用提供的包中的另一个 类 打印了一个节点,但随着我继续,我只是将 运行 保留在墙上。
下面的代码由于我的修改而变得混乱。我只是不知道从这里去哪里。截至目前,这是一个空指针异常。
仅供参考: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;
}
}
我认为你的核心问题在这里:
Node prev;
Node curr;
这些让你很困惑。删除它们。
Node prev;
- 这应该在Node
class.Node curr;
- 这应该是局部变量,而不是实例变量。
还有
Node head = new Node(null);
不与
胶凝if(head == null)
{
head = new Node(p);
使 head == null
表示列表为空或其他 - 但要保持一致。
(代表OP发表).
public void offer(Process p) {
if(head.process == null)
{
head = new Node(p);
first = head;
last = head;
}
last.next = new Node(p);
last = last.next;
}
这解决了我的问题。不敢相信我让这让我感到困惑。