我需要帮助为我的PerformanceList编写这个addAfterCurrent方法这是一个双重链接的列表,其中声明了标头的head,tail和游标(当前)节点。节点类使用
PerformanceNode接下来; PerformanceNode previous;
作为指针,他们也设置并获取方法 public void addAfterCurrent(PerformanceNode newPerformance)
方法定义 将新数据插入到PerformanceList中,以使新节点直接跟随当前节点(如果存在)。如果没有当前节点(即,当前节点为空),只需将该节点插入列表的末尾即可。当前节点现在应该是新创建的节点
我当前的方法没有在当前节点之后插入一个节点。这是我需要帮助的问题我无法在当前节点之后设置newPerformance
public void addAfterCurrent(PerformanceNode element)
{
PerformanceNode temp = element;
if (cursor == null)
{
head = temp;
tail = temp;
cursor = temp;
}
else
{
temp.setNext(cursor);
cursor.setPrev(temp);
cursor = temp;
if(cursor == null)
{
tail = cursor;
}
}
}
答案 0 :(得分:0)
我根据定义实施了一些评论:
public void addAfterCurrent(PerformanceNode node) {
if (node == null) {
throw new IllegalArgumentException("Node to be added cannot be null!");
}
if (cursor == null && tail == null) {
throw new IllegalStateException("There is neither current nor tail node!");
}
if (cursor == null) { // there is no current node
// insert the node at the end of the list
tail.setNext(node);
node.setPrev(tail);
// mode end cursor
tail = node;
// current node should now be the newly created node
cursor = node;
} else { // there is current node
PerformanceNode temp = cursor.getNext();
// new node directly follows the current node
cursor.setNext(node);
node.setPrev(cursor);
if (temp != null) {
node.setNext(temp);
temp.setPrev(node);
} else { // current node was the last node
tail = node; // mode end cursor
}
}
}