【发布时间】:2021-11-29 06:51:17
【问题描述】:
我现在正在处理一个双链表,我似乎无法打印链表包含的数据。
这是我的代码:
public class Box<E> {
private DoubleListNode<E> head;
private DoubleListNode<E> tail;
private int size;
public Box() {
head = new DoubleListNode<E>();
tail = new DoubleListNode<E>();
size = 0;
}
private static class DoubleListNode<E> {
private E myData;
private DoubleListNode<E> myNext;
private DoubleListNode<E> myPrev;
public DoubleListNode() {
this(null, null, null);
}
public DoubleListNode(DoubleListNode<E> prev, E data,
DoubleListNode<E> next) {
myData = data;
myNext = next;
myPrev = prev;
}
}
public void addLast(E data) {
DoubleListNode<E> newNode = new DoubleListNode<E>(tail.myPrev, data, tail);
if(size == 0) {
head = newNode;
} else {
tail.myNext = newNode;
newNode.myPrev = tail;
}
tail = newNode;
size++;
}
public void displayList() {
if (head == null) {
}
DoubleListNode<E> temp = head;
while (temp != null) {
System.out.print(temp.myData + " --> ");
temp = temp.myNext;
}
System.out.println("Null");
}
public static void main(String[] arg) throws FileNotFoundException {
Box<Integer> dl = new Box<Integer>();
dl.addLast(5);
dl.addLast(10);
dl.displayList();
}
当我尝试打印链接列表时,程序一直在运行,但没有打印任何内容。我做错了吗?
【问题讨论】:
-
尝试添加断点,看看程序何时卡住。 (在
displayList或addLast中):) [编辑:阅读代码后它不能卡在 addLast 中,但通常尝试了解问题发生在哪里] -
你熟悉一个叫做“调试器”的东西吗?此外,最古老的调试技术,打印到标准输出,仍然有效。