如何使用接口在双链表中编写添加方法? [关闭]

问题描述 投票:-1回答:1

我必须使用interface在List中编写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
}
java nodes doubly-linked-list
1个回答
1
投票

假设你有一个有点双链的列表,其中包含像(previous-node, next-node, stored-object)这样的节点:

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

  • N0位置得到节点index-1
  • N1位置得到节点index
  • Nn创建新节点(N0, N1, obj)
  • 将qazxsw poi qazxsw poi更新为qazxsw poi
  • 将qazxsw poi qazxsw poi更新为qazxsw poi
  • 返回true。
© www.soinside.com 2019 - 2024. All rights reserved.