表示方法は、私のプログラムは何も表示していません。
プログラムは、誰かの名前全体を循環リンクリストに入れ、リンクリストを別の循環リンクリストにバックアップする必要があります。
次に、ユーザーは名前が 1 つになるまで名前を削除する必要があり、入力された順序でバックアップを使用して、元の名前のリストと共に勝者を表示します。
import java.io.*;
import java.util.*;
import java.text.*;
public class Linkedlist
{
static public class Node
{
Node prev, next;
String data;
}
public static void delete (Node tail) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Please input a name to be deleted");
String tobedeleted = stdin.readLine ();
Node delete = tail;
while (delete.prev != null)
{
if (delete.data.equals (tobedeleted))
{
String temp = delete.data;
delete.data = tail.data;
tail.data = temp;
tail = tail.prev;
}
delete = delete.prev;
}
}
public static String findvictor (Node tail) throws IOException
{
int size = 0;
for (Node n = tail ; n.prev != null ; n = n.prev)
{
size++;
}
if (size == 1)
{
return tail.data;
}
else
{
delete (tail);
return findvictor (tail.prev);
}
}
public static void backup (Node tail, Node backuptail)
{
Node tobebackuped = tail;
Node backuphead = null;
Node backup = new Node ();
backuptail = backup;
while (tobebackuped.prev != null)
{
backup.data = tobebackuped.data;
backuphead = backup;
backup = new Node ();
backup.next = backuphead;
backuphead.prev = backup;
tobebackuped = tobebackuped.prev;
}
}
public static void display (Node tail, Node backuptail) throws IOException
{
System.out.println ("CONGRATULATIONS, " + findvictor (tail) + ", YOU ARE THE WINNER!");
System.out.println ("");
System.out.println ("This is a list of all the contestants:");
Node current = backuptail;
while (current.prev != null)
{
System.out.println (current.data);
current = current.prev;
}
}
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
Node head = null;
Node node = new Node ();
Node tail = node;
while (true)
{
String str = stdin.readLine ();
if (!str.equals ("fin"))
{
node.data = str;
head = node;
node = new Node ();
node.next = head;
head.prev = node;
}
else
{
break;
}
}
Node backuptail = null;
backup (tail, backuptail);
display (tail, backuptail);
}
}