如何从Java中的链接列表中删除?

时间:2022-08-02 17:32:36

So I wrote my own linked list (and list node) in Java as a part of a homework.

所以我用Java编写了自己的链表(和列表节点)作为家庭作业的一部分。

Now, I'm trying to erase entries, but the function is not working.

现在,我正在尝试删除条目,但该功能无效。

I know the concept:

我知道这个概念:

  • Search for node keeping the previous;
  • 搜索保留前一个节点;

  • Tell previous node to point to next node;
  • 告诉上一个节点指向下一个节点;

  • Return or stop using the node so GC erases it.
  • 返回或停止使用节点,以便GC擦除它。

For some reason it is not working. I can delete the node with the same value over and over. I'm afraid it is something related to Java pointers.

由于某种原因它不起作用。我可以一遍又一遍地删除具有相同值的节点。我担心它与Java指针有关。

The code:

Node:

public class SimpleNode<E> {

    private E value;
    private SimpleNode<E> next;

    public SimpleNode() {
        this.value = null;
        this.next = null;
    }

    public NoSimples(E data, SimpleNode<E> ref) {
        this.value = data;
        this.next = ref;
    }
// Getters and Setters
}

List:

public class LinkedList<E> implements Iterable<SimpleNode<E>> {

    private SimpleNode<E> head;
    private int size = 0;

    public LinkedList() {
        this.head = new SimpleNode<E>();
    }

    public void add(SimpleNode<E> node) {
        this.addFirst(node.getValue());
    }

    public void addFirst(E item) {
        SimpleNode<E> nonde = new SimpleNode<E>(item, this.head);
        this.head = node;
        size++;
    }

    public void add(E value) {
        this.addFirst(value);
    }

    public SimpleNode<E> removeFirst() {
        SimpleNode<E> node = this.head;
        if (node == null) {
            return null;
        } else {
            this.head = node.getNext();
            node.setNext(null);
            this.size--;
            return node;
        }
    }

    public SimpleNodes<E> remove(E value) {
        SimpleNode<E> nodeAnt = this.head;
        SimpleNode<E> node = this.head.getNext();
        while (node != null) {
            if (node.getValue()!= null && node.getValue().equals(value)) {
                nodeAnt.setNext(node.getNext());
                node.setNext(null);
                return node;
            }
            nodeAnt = node;
            node = node.getNext();
        }
        return null;
    }
// Other irrelevant methods.
}

1 个解决方案

#1


1  

Multiple Problems :

多个问题:

  1. Think if you have a list 1,2,3,4. Now, if you try to remove 1, your code fails.
  2. 想想你是否有1,2,3,4的清单。现在,如果您尝试删除1,则代码将失败。

  3. nodeAnt = node should be nodeAnt = nodeAnt.getNext(). Remember, the're all references, not Objects
  4. nodeAnt = node应该是nodeAnt = nodeAnt.getNext()。请记住,这些都是引用,而不是对象

Also, a recursive way might be easier to understand. For example, Here is how I implemented it

此外,递归方式可能更容易理解。例如,以下是我实现它的方式

public void remove(E e){
    prev = head;
    removeElement(e, head);
    System.gc();
}

private void removeElement(E e, Node currentElement) {
    if(currentElement==null){
        return;
    }
    if(head.getData().equals(e)){
        head = head.getNext();
        size--;
    }else if(currentElement.getData().equals(e)){
        prev.setNext(currentElement.getNext());
        size--;
    }
    prev = prev.getNext();
    removeElement(e, currentElement.getNext());

}

Note: I delete all occurrences of the Element, as I needed it. You may need it to be different.

注意:我删除了所有出现的Element,因为我需要它。你可能需要它与众不同。

#1


1  

Multiple Problems :

多个问题:

  1. Think if you have a list 1,2,3,4. Now, if you try to remove 1, your code fails.
  2. 想想你是否有1,2,3,4的清单。现在,如果您尝试删除1,则代码将失败。

  3. nodeAnt = node should be nodeAnt = nodeAnt.getNext(). Remember, the're all references, not Objects
  4. nodeAnt = node应该是nodeAnt = nodeAnt.getNext()。请记住,这些都是引用,而不是对象

Also, a recursive way might be easier to understand. For example, Here is how I implemented it

此外,递归方式可能更容易理解。例如,以下是我实现它的方式

public void remove(E e){
    prev = head;
    removeElement(e, head);
    System.gc();
}

private void removeElement(E e, Node currentElement) {
    if(currentElement==null){
        return;
    }
    if(head.getData().equals(e)){
        head = head.getNext();
        size--;
    }else if(currentElement.getData().equals(e)){
        prev.setNext(currentElement.getNext());
        size--;
    }
    prev = prev.getNext();
    removeElement(e, currentElement.getNext());

}

Note: I delete all occurrences of the Element, as I needed it. You may need it to be different.

注意:我删除了所有出现的Element,因为我需要它。你可能需要它与众不同。