I need to write a program that stores names in a circular linked list, then deletes them one by one until there is only one name left. I can't get my delete method to work, can someone help me figure out what's wrong? Here's the code for the method.
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));
}
Here's a sample of the output when inputting numbers in for the names.
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
There are many things wrong with your code. The following lines all do something else than what you think they do:
while (!node.next.equals (head) || found == false);
Does nothing (at best) or loops infinitely. I guess it should have reset the node
to head
but I fail to see why.
if (loser.equals (head))
{
...
}
Does nothing. In fact it shouldn't even compile without a big fat warning that the condition will never be true. You are comparing string objects to node objects here. I guess you would have wanted to "repair" head
in case of its deletion here.
if (found == true)
{
node = node.next;
temp = null;
System.out.println ("Elimination sucessful!");
}
Does not delete anything. It just advances the node
pointer. You probably need something along the lines of node.next = node.next.next
here, assuming node
being the predecessor of the loser temp
-- which it is not.
And finally...
System.out.println ("The contestants left are:");
do { System.out.println (node.data); node = node.next; }
while (!node.next.equals (head));
Does not print the remaining contestants (nodes in linked list) but merely those from the current cursor node
to the "end". I guess you wanted to iterate from head
till "end".
Good luck with your studies. Please tag your questions "homework" if they are.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.