繁体   English   中英

从链表中删除一个元素

[英]Remove an element from the linked list

如何从链表中删除一个元素?

是否正确:

public E remove(E element) {
        Node search = currentNode;
        boolean nonStop = true;
        while(((search.previous) != null) && (nonStop)) {
            if(search.previous.toString().equals(element.toString())) {
                System.out.println("Item found !!!");
                search.previous = search.previous.previous;
                nonStop = false;
            } else {
                search = search.previous;
            }
        }
        currentNode = search;
        return null;
    }

public class Node<E> {
    public E data;
    public Node<E> previous;

    public Node(E data) {
        this.data = data;
    }

    public void printNode() {
        System.out.println("Node details: "+data);
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return (String)data;
    }

}

问题是当我打印所有元素时,getAllElements()没有给出正确答案,remove()方法或getAllElements中是否有任何问题

public void getAllElements() {
        Node<E> aNode = currentNode;
        while(aNode != null) {
            aNode.printNode();
            aNode = aNode.previous;
        }
    }

线

if(search.previous.toString().equals(element.toString())

调用节点上而不是元素上的字符串。

看来您的remove方法实际上并没有删除任何内容,您应该更新方法中的指针,以使任何内容都指向您要删除的元素,然后垃圾回收器将删除所有未指向的元素。 一些伪代码来说明我的意思:

public remove(Element element){
  for (Element e : myLinkedList){
    if (e.equals(element)){
      if (next != 0)
        previousPtr = nextPtr;
      else
        previousPtr = null;
    }
  }
}

注意这不是正确的Java代码,只是伪代码给您一个想法,我为您节省了一些乐趣! :)

尝试这个:

public void remove(E element)
{
    Node n = head;  // This is the head of the linked list-- It is the starting node of your linked list: For your case "currentNode"
    Node tmp;
    while(n!=null && !n.data.equals(element))
    {
        tmp = n;
        n = n.previous;
    }

    if(n==null)
    {
        // Do your stuff
        System.out.println("Element "+element+" not found.");
    }
    else
    {
        // Do your stuff
        tmp.prev = n.prev;
        n.prev = null;
        System.out.println("Element "+element+" removed.");
    }
}


// Suggestion: This method name should be "printList()"
public void getAllElements()
{
    Node n = head;      // In your case: "currentNode"
    while(n!=null)
    {
        n.printNode();
        n = n.previous;
    }   
}

您的元素没有某种标识符吗? 然后,您可以将其简单得多,例如: 删除对象

public E remove(E element) {
        Node search = currentNode;
        boolean nonStop = true;
        while(((search.previous) != null) && (nonStop)) {
            if(search.previous.data.equals(element)) {
                System.out.println("Item found !!!");
                search.previous = search.previous.previous;
                nonStop = false;
            }
            search = search.previous;
        }
       return null;
    }

我已经找到了该问题的解决方案,感谢大家的支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM