2014-02-07 56 views
0

試圖編寫一個方法,該方法從單向鏈表中刪除值的所有實例,但似乎不起作用。單鏈接列表:正在刪除

我試圖適應的頭部是否包含的價值,但我不知道這是否是這樣做的正確方法:

public void remove (int value) 
{ 
    if (head.value == value) 
    { 
    head = head.next; 
    count--; 
    } 
    IntegerNode temp=head; 
    while (temp !=null) 
    { 
     if (temp.next != null) 
     { 
      if (temp.next.value == value) 
      { 
       temp.next = temp.next.next; 
       count--; 
      } 
     } 
     temp=temp.next; 
    } 
} 

有什麼明顯的毛病我碼?

+0

什麼問題exacly,爲什麼你正在使用count-- –

+1

的統計顯示多少個值在列表中。 – user3283585

+0

把if語句放在while循環之後 –

回答

0

這裏是不同的方式從鏈表中刪除

public Node removeAtFront() 
    { 
     Node returnedNode = null; 

     if(rootNode !=null) 
     { 

      if(rootNode.next !=null) 
      { 
       Node pointer = rootNode; 
       returnedNode = rootNode; 
       pointer = null; 
       rootNode = rootNode.next; 
      } 
      else 
      { 
       Node pointer = rootNode; 
       returnedNode = rootNode; 
       pointer = null; 
       rootNode = rootNode.next; 
       System.out.println("removing the last node"); 
      } 
     }else 
     { 
      System.out.println("the linkedlist is empty"); 
     } 

     return returnedNode; 
    } 

    public Node removeAtBack() 
    { 
     Node returnedNode = null; 

     if(rootNode != null) 
     { 
      //Remove the commented line if you wish to keep 1 node as minimum in the linked list 
      //if(rootNode.next !=null) 
      //{ 
       Node pointer = new students(); 
       pointer = rootNode; 

       while(pointer.next.next !=null) 
       { 
        pointer=pointer.next; 
       } 

       returnedNode = pointer.next.next; 
       pointer.next.next = null; 
       pointer.next = null; 
      //} 
      //else 
      //{ 
      // System.out.println("cant remove the last node because its the root node"); 
      //} 
     } 
     else 
     { 
      System.out.println("the linkedlist is empty"); 
     } 

     return returnedNode; 
    } 

    public Node removeNode(String name) 
    { 
     Node returnedNode = null; 

     if(rootNode !=null) 
     { 
      Node pointer = new students(); 
      Node previous = new students(); 

      pointer = rootNode; 


      while(pointer !=null) 
      { 
       if(pointer.name.equals(name)) 
       { 
        previous.next = pointer.next; 
        returnedNode = pointer; 
        pointer = null; 

        break; 
       } 
       else 
       { 
        previous = pointer; 
        pointer = pointer.next; 
       } 
      } 

     } 
     else 
     { 
      System.out.println("the linkedlist is empty"); 
     } 

     return returnedNode; 
    } 

    public boolean isEmpty() { 

     return rootNode == null; 
    } 
0

你需要跟蹤,你已經在列表中,而不是你要去哪裏。就像這樣:

public void remove (int value) 
{ 
    IntegerNode current = head; 

    while (current !=null) 
    { 
     if (current.value == value) 
     { 
      if (head == current) 
      { 
       head = current.next; 
      } 
      else 
      { 
       head.next = current.next; 
      } 
      count--; 
     } 
     current=current.next; 
    } 
} 
+0

什麼是「等於(當前)」應該完成的操作?這會比較previous.value current.value並返回true或false?我試圖用「previous.value == current.value」替換這個,這也不起作用。編輯:你的確切代碼也創建一個nullpointerexception。 – user3283585

+0

@ user3283585你是對的。我正在考慮這個問題,並改爲反對平等。它究竟在哪裏產生一個n?? – mikea

+0

奇怪的是,它似乎指向我的代碼的另一個區域,當它說「return temp.value;」時,我的「get」命令。 – user3283585

1

這裏鏈表的實現與addremove方法與測試。

public class ListDemo { 
    public static void main(String[] args) { 
     MyList list = new MyList(); 
     list.addToEnd(1); 
     list.addToEnd(2); 
     list.addToEnd(3); 

     list.removeByValue(2); 
     list.removeByValue(3); 
    } 

} 

class MyList { 
    private IntegerNode head; 
    private int count = 0; 

    public void addToEnd(int value) { 

     if(head == null) { 
      head = new IntegerNode(value); 
      count = 1; 
      head.next = null; 
      return; 
     } 
     IntegerNode current = head; 
     while (current.next != null) { 
      current = current.next; 
     } 
     IntegerNode node = new IntegerNode(value); 
     node.next = null; 

     count++; 
     current.next = node; 
    } 

    public void removeByValue(int value) { 
     if (count == 0) { 
      return; 
     } else if (count == 1) { 
      if (head.value == value) { 
       count = 0; 
       head = null; 
      } 

     } else { 
      IntegerNode current = this.head; 
      IntegerNode next = current.next; 
      while (next != null) { 
       if (next.value == value) { 
        if (next.next == null) { 
         current.next = null; 
         count--; 
         return; 
        } else { 
         current.next = next.next; 
         count--; 
        } 
       } 
       next = next.next; 
      } 
     } 
    } 
} 

class IntegerNode { 
    IntegerNode(int value) { 
     this.value = value; 
    } 

    IntegerNode next; 
    int value; 
} 
+0

如果我不調用add方法,但是調用只能移除拋出NullPointer的count,因爲它不能初始化。 – herry

+0

好吧,我現在看看吧 –

+0

我加了檢查'count == 0'的情況。 –