2013-12-19 64 views
0

我需要編寫一個程序來存儲循環鏈表中的名字,然後逐個刪除它們,直到只剩下一個名字。我無法讓我的刪除方法工作,有人可以幫我弄清楚什麼是錯的?這是該方法的代碼。圓形單鏈表刪除方法

public static void delete() throws IOException 
{ 
    String loser; 
    boolean found; 
    Node temp; 
    do 
    { 
     System.out.println ("Please enter the name of the contestant you want to eliminate"); 
     loser = stdin.readLine(); 
     head = node; 
     found = false; 
     do 
     { 
      node = node.next; 
      if (node.data.equals (loser)) 
      { 
       found = true; 
       temp = node; 
      } 
     } 
     while (!node.next.equals (head) || found == false); 
     if (loser.equals (head)) 
     { 
      head = node.next; 
     } 
     if (found == true) 
     { 
      node = node.next; 
      temp = null; 
      System.out.println ("Elimination sucessful!"); 
     } 
     else 
     { 
      System.out.println ("This name was not found! Please try again."); 
     } 
     System.out.println ("The contestants left are:"); 
     do 
     { 
      System.out.println (node.data); 
      node = node.next; 
     } 
     while (!node.next.equals (head)); 
     if (node.next.equals (node)) 
     { 
      System.out.println ("There is only one contestant left!"); 
     } 
    } 
    while (!node.next.equals (node)); 
} 

下面是爲名稱輸入數字時的輸出示例。

Please enter the name of the contestant or 'fin' to stop: 
    1 
    Please enter the name of the contestant or 'fin' to stop: 
    2 
    Please enter the name of the contestant or 'fin' to stop: 
    3 
    Please enter the name of the contestant or 'fin' to stop: 
    4 
    Please enter the name of the contestant or 'fin' to stop: 
    5 
    Please enter the name of the contestant or 'fin' to stop: 
    fin 

    Please enter the name of the contestant you want to eliminate 
    1 
    Elimination sucessful! 
    The contestants left are: 
    5 
    1 
    2 
    3 
    Please enter the name of the contestant you want to eliminate 
    3 
    Elimination sucessful! 
    The contestants left are: 
    4 
    5 
    1 
    2 
    Please enter the name of the contestant you want to eliminate 
    4 
    Elimination sucessful! 
    The contestants left are: 
    3 
    4 
    5 
    1 
    Please enter the name of the contestant you want to eliminate 
    6 
+0

看看下面的帖子http://stackoverflow.com/questions/12338868/circular-linkedlist-implementation-in-java – sasankad

回答

0

代碼有很多錯誤。以下幾行都會做別的事情,而不是你認爲他們做的事:

while (!node.next.equals (head) || found == false); 

什麼都沒有(最好)或無限循環。我想它應該已將node重置爲head,但我不明白爲什麼。

if (loser.equals (head)) 
{ 
    ... 
} 

什麼都不做。事實上,它甚至不應該在沒有大的警告的情況下進行編譯,以免條件永遠不會成立。您在此處將字符串對象與節點對象進行比較。我想你會想在這裏刪除的情況下「修復」head

if (found == true) 
{ 
    node = node.next; 
    temp = null; 
    System.out.println ("Elimination sucessful!"); 
} 

不刪除任何東西。它只是推進node指針。假設node是輸家temp的前身 - 你不需要沿着node.next = node.next.next這一行發生。

最後...

System.out.println ("The contestants left are:"); 
do { System.out.println (node.data); node = node.next; } 
while (!node.next.equals (head)); 

不打印剩下的選手(在鏈表節點),但僅僅是那些從當前光標node到「結束」。我想你想從head迭代到「結束」。

祝你好運與你的研究。如果有問題,請給你的問題「家庭作業」加標籤。