我的Node
类,代表链表的一个节点,定义如下:
public class Node
{
Node next;
String data;
public Node (String data)
{
this.data = data;
}
}
我正在这样使用它:
Node node, head, tail;
String name; // name to be entered
int count = 0;
// initialize the head to null
head = null;
do
{
System.out.print ("Enter a name. Type q to end.");
name = stdin.readLine ();
// create a new node if the user doesn't type q
if (!name.equals ("q"))
{
node = new Node (name);
node.next = head;
count++;
// update the head to point to the new front of the list
head = node;
}
}
while (!name.equals ("q")); // loop continues until "quit" selected
node = head;
假设我想将名称备份到一个方法中,以防我修改原始列表。我怎样才能做到这一点?无需将其写入文件。
Name 是存储在链接列表中的变量,在用户按下 q 后,我想修改列表,同时保留用户存储的内容作为备份,以防他/她想要回溯或查看原始列表。