繁体   English   中英

从双向链接列表中删除光标

[英]Removing cursor from a doubly linked list

我正在尝试从列表中删除光标,并使其引用先前的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();
}

您的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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM