如何使用接口在双链表中编码添加方法



>我必须使用界面在列表中对 add 方法进行编码。 我试过了,但它甚至不起作用。 谁能帮我。 接口和列表类给出如下:

/**
 * Inserts the specified element at the specified position in this list. 
 *  Shifts the element currently at that position (if any) and any subsequent 
 *  elements to the right (adds one to their indices).
 * @param index Index at which to add
 * @param obj The object to add
 * @return True if insertion was successful
 */
public boolean add(int index, Token obj) {
    // implement this
}

假设你有一个双链表,其中包含如下节点(previous-node, next-node, stored-object)

伪代码(仅对index>0index<list.size有效(

  • 获取index-1位置的节点N0

  • 获取index位置的节点N1

  • 使用 (N0, N1, obj)创建新的节点Nn

  • N0 next-node更新为Nn

  • N1 previous-node更新为Nn

  • 返回 true。

最新更新