我试图从列表中删除光标并使其引用前一个CarListNode(或者如果光标先前引用了列表的头部,则为头部)。同时仍然返回光标内的信息。 我的代码没有正确删除光标。我的代码中有什么问题?
这是我目前的代码:
public Fruit removeCursor() throws EndOfListException {
if (cursor == null) {
throw new EndOfListException();
} else if (cursor.getPrev() == null) {
head = cursor.getNext();
cursor.setNext(null);
cursor.setPrev(null);
cursor = head;
} else if (cursor.getNext() == null) {
tail = cursor.getPrev();
cursor.setPrev(null);
cursor.setNext(null);
cursor = tail;
} else {
cursor.setData(cursor.getNext().getData()); //this isn't a singly linked list
cursor.setNext(cursor.getNext().getNext());
}
count--;
return cursor.getData();
}
答案 0 :(得分:0)
您的else
子句没有"删除光标" ...
尝试这样的事情:
else {
cursor.getPrev().setNext(cursor.getNext());
cursor.getNext().setPrev(cursor.getPrev());
// You might want to release the cursor's item, EG:
cursor = null;
cursor = cursor.getNext();
}