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
看看下面的帖子http://stackoverflow.com/questions/12338868/circular-linkedlist-implementation-in-java – sasankad