簡體   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