我试图使用具有我希望删除的特定索引的方法从列表中删除对象。这里棘手的部分是这个列表是一个双链表,当我从中删除一个节点时,需要将下一个和上一个指针重定向到正确的节点。这是我到目前为止所得到的,但代码似乎没有正确重定向指针,我会应用任何输入!
private static final class Node<T>
{
private T value;
private Node<T> previous, next;
private Node(T value, Node<T> previous, Node<T> next) // constructor
{
this.value = value;
this.previous = previous;
this.next = next;
}
}
private Node<T> head; // first in the list
private Node<T> tale; // last in the list
public T remove(int index) {
indexcontrol(index); // checks if legal index
Node<T> q, p = null;
if(index == 0)
{
q = head;
head = head.next;
}
else
{
p = findNode(index-1); // finds the nodes value on place index
q = p.next;
p.next= q.next;
}
if ( q== tale) tale = p;
T value = q.value;
q.value = null;
q.next = null;
return value;
}