我不是100%确定为什么我在链表实现上运行traverse()方法时出现堆栈溢出错误.如果我注释掉traverse()方法,程序运行正常.
我通过迭代使用size变量并在traverse方法中创建一个计数器变量来进行双重检查,但我仍然遇到堆栈溢出错误.
例如
@Override
public void traverse() {
Node<T> data = this.head; // In order traversal
int counter = 0;
while (counter < size) {
System.out.println(data.toString());
data = data.getNextNode();
counter++;
}
}
Run Code Online (Sandbox Code Playgroud)
Linked List类
public class LinkedList<T extends Comparable<T>> implements List<T> {
private Node<T> head; // First element of linked list
private Node<T> tail; // Last element of linked list
private int size;
public LinkedList() {
this.size = 0;
}
/**
* TODO: Implement iterator and ForEach methods later on …Run Code Online (Sandbox Code Playgroud)