如何从排序的双向链表中删除项目?

时间:2018-11-14 20:51:53

标签: java doubly-linked-list

我有一个排序的双向链接列表,该列表始终以第一个元素为null并以最后一个元素为null结束。当列表为空时,它看起来像这样:{null,null}。当我插入值a,b,c时,列表看起来像这样:{null,a,b,c,null}。

我有一个removeFirst()方法,该方法应该从列表中删除第一项,并且列表应如下所示:{null,b,c,null}。并返回删除的项目。

我还有一个removeLast()方法,该方法删除最后一个元素,因此列表应如下所示:{null,b,null}。并返回删除的项目。

removeElement()方法应该在任何地方从列表中删除一个项目,并返回true。如果该项目不在列表中,则返回false。

我现在使用的代码的问题是removeFirst()从列表中删除了第一个为空的项目。我对removeLast()方法也有同样的问题,它还会删除列表中最后一个为null的元素。但是列表中的第一个和最后一个元素应始终为null,并且不应删除。

removeElement()方法不会从列表中删除任何项目。我看不到我想念的东西。

到目前为止,这是我的代码:

// removes the first item in the list and returns the string
public String removeFirst() {

    // if the list is empty
    if (this.size == 0) {
        throw new NoSuchElementException();
    }

    Node tempNode = first;

    // if there is only one item in the list
    if (first.next == null) {
        last = null;
    } else {
        first.next.previous = null;
        first = first.next;
    }

    size--;

    return tempNode.data;
}

// removes the last item in the list and returns the string
public String removeLast() {

    // if the list is empty
    if (this.size == 0) {
        throw new NoSuchElementException();
    }

    Node tempNode = last.previous;

    // if there is only one item in the list
    if (first.equals(last)) {
        first.previous = null;
        first.next = last;
        last.previous = first;
        last.next = null;

        tempNode.next = null;

    } else {
        last.previous.next = null;
        last = last.previous;
        tempNode.previous = null;
    }

    size--;

    return tempNode.data;
}

// removes an item from the list
public boolean removeElement(String element) {
    boolean removed = false;

    try {
        // if the first element in the list should be removed
        if (first.data.equals(element)) {
            // if there is only one item in the list
            if (first.equals(last)) {
                last = null;
                first = first.next;

            } else {
                first.next.previous = null;
                first = first.next;
            }

            size--;
            removed = true;

            // if the last item in the list should be deleted
        } else if (last.data.equals(element)) {
            last.previous.next = null;
            last = last.previous;

            size--;
            removed = true;

        } else {
            Node before = first;
            Node after = first.next;

            // as long as there are more elements in the list
            while (first.next != null) {
                if (after.data.compareTo(element) == 0) {

                    before.next = after.next;
                    after.next.previous = before.next;

                    size--;
                    removed = true;

                    break;
                }

                before = after;
                after = after.next;
            }

        }
    } catch (NullPointerException e) {

        removed = false;
    }

    return removed;
}

0 个答案:

没有答案