gpt4 book ai didi

java - 为什么使用 "node.getNext( ) == null"作为失效节点的约定?

转载 作者:行者123 更新时间:2023-12-01 19:44:16 25 4
gpt4 key购买 nike

这是《Java 中的数据结构和算法(第六版)》一书的代码片段,该方法是 LinkedPositionalList 实现的一部分。我不明白该行到底是什么意思: “if (node.getNext( ) == null)//失效节点的约定”是为了.我希望得到一些有关其功能的解释。

// private utilities
/** Validates the position and returns it as a node. */

private Node<E> validate(Position<E> p) throws IllegalArgumentException {

if (!(p instanceof Node)) throw new IllegalArgumentException("Invalid p");

Node<E> node = (Node<E>) p; // safe cast

if (node.getNext( ) == null) // convention for defunct node
throw new IllegalArgumentException("p is no longer in the list");

return node;
}

最佳答案

您所询问的书中的实现使用了一个显式的 trailer 节点,该节点表示结构中的“最后一个”位置。因此,列表中的所有节点都必须有下一个节点。因此,如果节点的 getNext() 方法返回 null,则该节点不在列表中。

关于java - 为什么使用 "node.getNext( ) == null"作为失效节点的约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54053745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com