【问题标题】:Removing cursor from a doubly linked list从双向链表中删除光标
【发布时间】:2017-09-26 03:04:07
【问题描述】:

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

【问题讨论】:

  • 你有什么问题?
  • 抱歉,深夜。我只是问我的代码有什么问题,因为光标没有按预期删除。
  • 我的代码没有正确移除光标。我的代码有什么问题?在你告诉它之前谁知道光标是什么,
  • 我认为你应该改用 node 或者解释一下 cursor 是什么意思...

标签: java linked-list computer-science doubly-linked-list


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多