2014-11-16 80 views
1

我有一個Java類,其由節點列表的,WordNode,其具有類Word和稱爲next一個WordNode對象用作參考到下一個節點的屬性,如下所示:爪哇鏈表去除最後一個節點

class WordNode 
{ 
    Word word; 
    WordNode next; 

    WordNode(Word w) 
    { 
     word = w; 
     next = null; 
    } 

    Word getWord() 
    { 
     return word; 
    } 
} 

和類Word有一個名爲name字符串:

class Word 
{ 
    String name; 

    Word(String n) 
    { 
      this.name = n; 
    } 

    public String getName() 
    { 
      return name;  
    } 

    public void setName(String n) 
    { 
     name = n; 
    } 
} 

我有一個類,這是一個自定義的鏈表,而我必須添加和刪除SPECI話拼詞的名稱。我可以添加沒有問題,但是當我想刪除我有一些問題。下面是方法來刪除:

boolean remove(Word w) 
{ 
    WordNode wm = new WordNode(w); 

    if (list == null) return false; //can't delete on an empty list 
    else 
    { 
     WordNode aux = list; 

     while(aux != null) 
     { 
      if (wm.word.getName().compareTo(aux.word.getName()) == 0) //if the word to delete is found 
      { 
       if (aux.next == null) //to erase the last element 
       { 
        aux = null;  
       } 
       else 
       { 
        aux.word.setName(aux.next.word.getName()); //set current node's name to equal next node's 

        WordNode temp = aux.next.next; 
        aux.next = null; //to erase current node 
        aux.next = temp; //re-refer 
       }       
       return true; 
      } 
      else aux = aux.next; 
     } 

     return false; //reachable if word is not found 
    } 
} 

list應該是包含所有節點的鏈表。 aux是一個輔助列表,它將循環使用list以避免解除鏈接。所以,如果我選擇刪除WordNode,我會比較名稱。它實際上是消除以及當節點是在任何地方,除了最後一個節點:

if (aux.next == null) //to erase the last element 
{ 
    aux = null;  
} 

我希望能作出這樣的節點空標誌着一個新的最終的名單,但是它不得到擦除。我可以改變什麼來清除最後一個元素?預先感謝您的任何幫助/建議

+1

aux = null只是將對象aux的引用設置爲null。最好的辦法就是aux.prev.next = null – Ben

+0

雖然我沒有'prev'的字段。但是這不是和'aux = null'一樣嗎? – gfcf14

+0

'if(aux.next == null)'只是告訴你你的aux是列表中的最後一個節點。將最後一個元素的引用設置爲null不會影響前一個節點,因爲它的下一個節點將引用您認爲已刪除的元素。這就是爲什麼你需要跟蹤你的前一個節點。 – Kalenda

回答

2

你必須清除WordNode的「下一個」指針。由於您沒有「上一個」指針,因此必須手動跟蹤上一個WordNode。

boolean remove(Word w) 
{ 
    WordNode wm = new WordNode(w); 

    if (list == null) return false; //can't delete on an empty list 
    else 
    { 
     WordNode aux = list; 
     WordNode prev = aux; 

     while(aux != null) 
     { 
      if (wm.word.getName().compareTo(aux.word.getName()) == 0) //if the word to delete is found 
      { 
       if (aux.next == null) //to erase the last element 
       { 
        prev.next = null; 
        // Takes care of the case of a one-item list 
        aux = null; 
       } 
       else 
       { 
        aux.word.setName(aux.next.word.getName()); //set current node's name to equal next node's 

        WordNode temp = aux.next.next; 
        aux.next = null; //to erase current node 
        aux.next = temp; //re-refer 
       }       
       return true; 
      } 
      else { 
       prev = aux; 
       aux = aux.next; 
     } 

     return false; //reachable if word is not found 
    } 
} 
+0

這工作!但是,當列表中只有一個元素時不適用。所以我加了'if(prev == aux)list = null;'if before(aux.next == null)'我測試了它並沒有發現任何錯誤,但是你認爲我錯過了什麼嗎?無論如何,非常感謝你,其餘的代碼工作! – gfcf14

+0

我建議這樣做是正確的,這是添加一個'頭'節點。如果你所謂的列表只是第一個節點,並且你將它設置爲null,那麼你的列表就不存在了。您無法將更多節點插入列表中。你可以在插入新節點之前檢查你的列表以檢查if(list == null){list = newNode},以便讓你的列表再次運行,如果它是null的話,但事情是它會變成一個新的列表以及之前所有對它的引用都將保留爲null,而不是反映插入新節點的更改。 – marcelv3612

+0

啊,我的不好。 aux是列表的副本,因此將其設置爲null對實際列表對象沒有影響。 我認爲現在我們保留了一個前面的節點,我們可以在一定程度上簡化刪除。 prev.next = aux.next應該完成這項工作。 另外,就像馬塞爾夫說的,我會把一個「頭」。任何體面的清單實施應該有。 – Wehrdo

0

添加一個head節點。 然後在你的支票最後一個節點,你將有:

if (aux.next == null) //to erase the last element 
{ 
    aux.head.next = null; 
} 
+0

所以,因爲它指的是第一個節點,所以'head'總是「aux」嗎? – gfcf14