public void set(int index, T item) {
if (index < 0 || index >= this.size()) throw new IndexOutOfBoundsException();
Node<T> tmp1 = front;
for (int i = 0; i < index; i++) {
tmp1 = tmp1.next;
}
Node<T> tmp2 = new Node<T>(tmp1.prev, item, tmp1.next);
tmp1.prev.next = tmp2;
tmp1.next.prev = tmp2;
}
有人可以帮我弄清楚如何解决这个问题吗?
答案 0 :(得分:1)
以下是一些应该有效的sudo代码。
set(index, item)
{
Node<T> tmp1 = front;
for (int i = 0; i < index; i++) {
tmp1 = tmp1.next;
}
Node beforeNewNode = tmp1.prev// tmp1 is in the index we want to replace so we need the node before tmp1. That node is tmp1.prev
Node afterNewNode = tmp1.next//tmp1.next will be the node after the new node
Node newNode = new Node(beforeNewNode, item, afterNewNode);
beforeNewNode.next = newNode//connect the new node to the node that should be in front of it
afterNewNode.prev = newNode//connect the new node to the node that should come after it.
}