Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 双链表-移除第一种方法_Java_Linked List_Doubly Linked List - Fatal编程技术网

Java 双链表-移除第一种方法

Java 双链表-移除第一种方法,java,linked-list,doubly-linked-list,Java,Linked List,Doubly Linked List,我需要以下代码的帮助: public boolean remove(Integer value) { if (isEmpty()) { throw new NoSuchElementException(); } if (!isEmpty()) { if (head == tail) { head = tail = null; } } else { } size--;

我需要以下代码的帮助:

public boolean remove(Integer value) {

    if (isEmpty()) {
        throw new NoSuchElementException();
    }
    if (!isEmpty()) {
        if (head == tail) {
            head = tail = null;
        }
    } else {


    }

    size--;

    return false;
}
这就是我的任务:

“从此列表中删除第一次出现的指定值”

这是一种双链表的方法

到目前为止,我认为我做的是正确的,但我仍然错过了“其他”部分,我不知道该放在里面什么

我还有一个带有构造函数、getter和setter方法的类

这是我的节点类:

public class ListElement {

    private Integer value;
    private ListElement next;
    private ListElement prev;

    public ListElement(ListElement prev, Integer value, ListElement next) {
        this.value = value;
        this.next = next;
        this.prev = prev;
    }

    public Integer getValue() {
        return value;
    }

    public ListElement getNext() {
        return next;
    }

    public ListElement getPrev() {
        return prev;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public void setNext(ListElement next) {
        this.next = next;
    }

    public void setPrev(ListElement prev) {
        this.prev = prev;
    }

}

首先,应该删除该
if
,因为您已经测试了空值

if (!isEmpty()) {
    if (head == tail) {
        head = tail = null;
    }
} else {


}
然后您可以像这样处理:

public boolean remove(Integer value) {
    if (isEmpty()) {
        throw new NoSuchElementException();
    }

    if (head == tail) {
        if (head.getValue() == value) {
            head = tail = null;
        } else  {
            // Not found, return false
            size--;
            return false;
        }
    }

    ListElement current = head;
    while (current != null) {
        if (current.getValue() == value) {
            // Found
            if (current.getPrev() == null) {
                // current node is head node
                head = current.getNext();
                current.setPrev(null);
            } else if (current.next() == null) {
                // current node is tail node
                tail = current;
                current.setNext(null);
            } else {
                // Current node is in the middle
                ListElement prev = current.getPrev();
                ListElement next = current.getNext();
                prev.setNext(next);
                next.setPrev(prev);
            }
            size--;
            return true;
        }
    }
    // Not found
    size--;
    return false;
}

我通过函数签名假设您想要删除具有特定值的元素。您需要找到该节点并将其删除:

public boolean remove(Integer value) {

    if (isEmpty()) {
        throw new NoSuchElementException();
    }
    ListElement found = head;
    // Try to find it
    while (null != found && !found.value.equals(value)) {
         found = found.next;
    }
    // Not found?
    if (null == found) {
        throw new NoSuchElementException();
    }
    // Found. Unlink
    if (found.prev != null) found.prev.next = found.next;
    else head = found.next;
    if (found.next != null) found.next.prev = found.prev;
    else tail = found.prev;

    size--;

    return true;
}

您需要找到包含
值的节点
,然后取消链接。你的
if/else
需要删除,因为第一个
if(isEmpty())
处理这个问题。我想你希望else在第二个if语句中。@Adam:是的,先生!已修复该部分。请发布您的节点类
[教我]
请询问您的老师。所以我会为你提供一些代码,但是一个真正的老师会做得更好。谢谢你的帮助,但是代码不起作用!?我觉得这很让人困惑。老实说,我很难理解。