0

如何从链表中删除元素?

方法是否正确:

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;
        }
    }
4

5 回答 5

1

尝试这个:

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;
    }   
}
于 2013-07-12T09:08:30.440 回答
1

线

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

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

于 2013-07-12T07:42:15.697 回答
1

看起来您的remove 方法并没有真正删除任何内容,您应该更新方法中的指针,以便没有任何内容指向您要删除的元素,然后垃圾收集器将删除没有内容指向的元素。一些伪代码来说明我的意思:

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

注意这不是正确的Java代码,只是伪代码给你一个想法,我给你留点乐趣!!:)

于 2013-07-12T08:20:45.007 回答
0

你的元素没有某种标识符吗?然后你可以做的更简单,就像这里:删除一个对象

于 2013-07-12T07:40:54.763 回答
0
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;
    }

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

于 2013-07-12T09:35:35.257 回答