2011-10-15 64 views
0

我必須編寫這些方法的代碼。這是一項家庭作業,不能改變方法的參數,或者寫其他方法。 contains()和isEmpty()正常工作。 removeFirst()和removerLast()也可以很好地工作。爲什麼LinkedList的removeAll()和removeFirstOccurrence()刪除節點?

removeFirstOccurence()不會刪除指定元素的第一個成員。 而removeAll()不會取消所有指定的元素。

/** 
* Removes the first occurrence of the specified element in this list (when 
* traversing the list from head to tail). * 
* @param value element to be removed from this list, if present 
* @return {@code true} if the list contained the specified element 
*/ 
public boolean removeFirstOccurrence(int value) { 
    if(!contains(value)) 
     return false; 
    else{ 
     boolean result = false; 
     Node current = head; 
     while ((current != null) && !result) { 
      if (current.value == value){ 
       current=current.next; 
       size--; 
       return true; 
       } 
      current = current.next; 
     } 
     return result; 
    } 
} 
/** 
* Removes all occurrences of the specified element from this list. 
* @param value the element to remove 
* @return {@code false} if nothing changed, otherwise {@code true} 
*/ 
public boolean removeAll(int value) { 
    if(isEmpty()) 
     return false; 
    else{ 
     boolean result = false; 
     Node current = head; 
     while ((current.next != null) && !result) { 
      if (current.value == value){ 
       current=current.next; 
       size--; 
       result=true; 
      }     
      current= current.next; 
     } 
     return result; 
    } 
} 

這裏MyLinkedList類的第一部分:

public class MyLinkedList { 

private class Node { 

    private int value; 
    private Node next; 

    private Node(int value) { 
     this.value = value; 
     this.next = null; 
    } 

    @Override 
    public String toString() { 
     ... 
    } 
} 

private Node head; 
private int size; 

//和方法...

+1

你在哪裏更改當前節點的下一個值? –

回答

0

你永遠不會改變任何節點以任何方式

public boolean removeFirstOccurrence(int value) { 
    //don't do contains(), you are looping over the entire thing anyway doing it again is useless 
    if(head==null)return false; 
    if(head.value==value){//does head contain the value 
     head=head.next; 
     size--; 
     return true; 
    } 
    Node prev = head; 
    while ((prev.next != null)) {//actually checking prev.next if it contains the value 
     if (prev.next.value == value){ 
      prev.next=prev.next.next; //remove link from prev to prev.next 
      size--; 
      return true; 
     } 
     prev = prev.next; 
    } 
    return false; 
} 

public boolean removeAll(int value) { 
    if(head==null)return false; 
    while(head.value==value){//does head contain the value 
     head=head.next; 
     size--; 
     result=true; 
    } 
    Node prev=head; 
    while ((prev.next != null)) {//actually checking prev.next if it contains the value 
     (prev.next.value == value){ 
      prev.next=prev.next.next; //remove link from prev to prev.next 
      size--; 
      result = true; 
     }else { 
      prev = prev.next; 
     } 
    } 
    return result; 
} 
+0

現在我明白如何從pret到prev.next刪除鏈接:)。 但removeAll()得到了NullPointerException。我試過迭代,其中的條件是:(prev.next.next!= null)螞蟻它沒有得到NullPointerException,但最後兩個指定的元素不刪除 – blaces

+0

@blaces添加一個'if(head == null)return false;'在每個函數的開頭,當head爲null時,我把前進放到else中的下一個節點上(因爲否則prev.next已經改變了 –

+0

謝謝:)它運行良好,現在我不知道算法:) – blaces