簡體   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