问题描述
我目前正在研究 Java 中的循环双向链表。但是,我的教授说我们不要在我们的程序中使用名为 last(tail) 的字段。我还能如何引用最后一个节点?由于它是循环的,我知道我们可以使用第一个节点访问最后一个节点,但是如何?下面是我的 addLast 方法的代码,我必须替换最后一个变量,因为我们不允许有 last。不允许为最后一个节点创建字段。
public void addLast(T element) {
//TODO create the new node
DoublyLinkedNode<T> tmp = new DoublyLinkedNode<>(element);
//TODO adjust references for new node
if (numNodes == 0) {
first = tmp;
first.setPrev(first);
first.setNext(first);
last = first;
last.setNext(first);
}
last.setNext(tmp);
tmp.setPrev(last);
first.setPrev(tmp);
tmp.setNext(first);
last = tmp;
//TODO increase the numNodes variable
numNodes+=1;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)