Java 基本列表节点分配

Java 基本列表节点分配,java,Java,我在一个Java数据结构入门类中,遇到了一个分配给该类的问题。问题如下: 根据规范完成以下方法 // Insert a new Listnode<E> containing d after the first n Listnodes in // chain. If n is less than 1 or bigger than the number of Listnodes in // chain, throw IndexOutOfBoundsException. // Pre

我在一个Java数据结构入门类中,遇到了一个分配给该类的问题。问题如下:

根据规范完成以下方法

// Insert a new Listnode<E> containing d after the first n Listnodes in 
// chain. If n is less than 1 or bigger than the number of Listnodes in 
// chain, throw IndexOutOfBoundsException. 
// Precondition: chain has at least one node 
// 
// Examples: 
// chain: "A" n: 1 d: "B" chain: "A" -> "B" 
// chain: "A" -> "B" n: 1 d: "C" chain: "A" -> "C" -> "B" 
// chain: "A" n: 2 d: "B" IndexOutOfBoundsException 
public static <E> void insertAfter(Listnode<E> chain, int n, E d) { 
//在中的前n个Listnodes之后插入一个包含d的新Listnode
//链子。如果n小于1或大于中的Listnodes数
//链,抛出IndexOutOfBoundsException。
//前提条件:链至少有一个节点
// 
//示例:
//链:“A”n:1d:“B”链:“A”->“B”
//链:“A”->“B”n:1d:“C”链:“A”->“C”->“B”
//链:“A”n:2D:“B”索引自动边界异常
公共静态void insertAfter(Listnode链,int n,E d){

我知道我需要做的第一件事是计算出“链”中有多少个节点,为了做到这一点,有一个for循环(或者while循环?)还有一个临时变量,但我不知道接下来该怎么办?你能帮我吗?谢谢:)

你至少可以试试。看看是否有效,然后再回来!你不需要知道链中有多少节点,链表的要点是动态的(需要时添加和删除元素)。所需的只是指向链中下一个元素的指针。如果指针为空,则已到达列表的末尾。