如何在java中复制链表?

如何在java中复制链表?,java,linked-list,Java,Linked List,MyNode类表示链表的节点,定义如下: 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

My
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; 
我是这样使用它的:

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键后,我想修改列表,同时保留用户存储的内容作为备份,以防他/她想回溯或查看原始列表。

因此听起来好像您想保留链接列表中每个元素的先前名称的历史记录。我建议您在链表的每个节点中存储一个数组或链表,以显示该项以前的历史记录。例如:

public class Node
    {
    Node next;
    String data;
    LinkedList<String> history;

    public Node (String data)
    {
        this.data = data;
    }
}
公共类节点
{
节点下一步;
字符串数据;
链接列表历史;
公共节点(字符串数据)
{
这个数据=数据;
}
}
您可以通过多种方式填充它,这一切都取决于您的用例

还有,为什么要实现自己的链表?Java已经附带了一个链表实现(Java.util.LinkedList)。如果您需要链表种类的有序列表,我建议您使用此选项。如果执行了此操作,则创建一个包含名称和历史记录的新数据结构,然后仅在其中维护历史记录,例如:

public class DataItem
    {
    String data;
    LinkedList<String> history = new LinkedList<>();

    public DataItem (String data)
    {
        this.data = data;
    }

    public void setData (String data)
    {
        this.history.add(0, this.data);
        this.data = data;
    }
}
公共类数据项
{
字符串数据;
LinkedList history=新建LinkedList();
公共数据项(字符串数据)
{
这个数据=数据;
}
公共void setData(字符串数据)
{
this.history.add(0,this.data);
这个数据=数据;
}
}
最后,记住字符串在Java中是不可变的。因此,不能修改字符串。您只需要在某个地方保留对先前字符串的引用,而不需要复制该值


要最终复制一个对象树,您需要进行所谓的深度复制,基本上要遍历整个结构和所有集合,并将每个对象克隆到一个新对象中。

因此听起来好像您想要保留链接列表中每个元素的先前名称的历史记录。我建议您在链表的每个节点中存储一个数组或链表,以显示该项以前的历史记录。例如:

public class Node
    {
    Node next;
    String data;
    LinkedList<String> history;

    public Node (String data)
    {
        this.data = data;
    }
}
公共类节点
{
节点下一步;
字符串数据;
链接列表历史;
公共节点(字符串数据)
{
这个数据=数据;
}
}
您可以通过多种方式填充它,这一切都取决于您的用例

还有,为什么要实现自己的链表?Java已经附带了一个链表实现(Java.util.LinkedList)。如果您需要链表种类的有序列表,我建议您使用此选项。如果执行了此操作,则创建一个包含名称和历史记录的新数据结构,然后仅在其中维护历史记录,例如:

public class DataItem
    {
    String data;
    LinkedList<String> history = new LinkedList<>();

    public DataItem (String data)
    {
        this.data = data;
    }

    public void setData (String data)
    {
        this.history.add(0, this.data);
        this.data = data;
    }
}
公共类数据项
{
字符串数据;
LinkedList history=新建LinkedList();
公共数据项(字符串数据)
{
这个数据=数据;
}
公共void setData(字符串数据)
{
this.history.add(0,this.data);
这个数据=数据;
}
}
最后,记住字符串在Java中是不可变的。因此,不能修改字符串。您只需要在某个地方保留对先前字符串的引用,而不需要复制该值


要最终复制一个对象树,您需要进行所谓的深度复制,基本上是遍历整个结构和所有集合,并将每个对象克隆到一个新对象中。

最好使节点不可变。因此,每次要修改节点时,都要创建一个新节点。并将旧节点存储在链接列表历史记录中。

最好使节点不可变。因此,每次要修改节点时,都要创建一个新节点。并将旧的存储在链接列表历史记录中。

这种方法存在一些问题,一个是封装。我希望一个类的实现细节(读:代码)在一个类中,并且有适当的访问限制,以防止外部过程弄乱列表。也;这看起来像家庭作业。我同意这看起来像家庭作业。如果是,请贴上标签。请参阅@squawknull-“家庭作业”标记已弃用。啊,谢谢,我没有意识到……这种方法有一些问题,一个是封装。我希望一个类的实现细节(读:代码)在一个类中,并且有适当的访问限制,以防止外部过程弄乱列表。也;这看起来像家庭作业。我同意这看起来像家庭作业。如果是,请贴上标签。请参阅@squawknull-“家庭作业”标记已弃用。啊,谢谢,我不知道。。。