复制链表中的节点 java

duplicating a node in a linked list java

刚开始,这是作业,感谢您提前提供帮助。我一直卡在小问题上,所以我希望你们能帮我解决一个问题。我想做的是创建一个具有多个函数的链表。我遇到的问题是排序(我可以做其他的)。每个节点包含一个字符串、一个整数和一个双精度数。根据用户的要求,我需要能够按其中的每一个以及输入的顺序进行排序。 ***同样重要的是,我的对象中的变量是私有的,我的对象名为 list1。基本上,我必须为时间顺序制作一个链表,并为其他顺序制作一个链表。

我的计划是在用户输入节点时按正确顺序插入节点。因此,当用户输入一个节点时,该节点需要进入按时间顺序排列的列表和其他列表中的正确位置。所以,我需要复制节点来做到这一点。但是,我不能简单地说

icopy(copy for integer) = newNode(node the user just inputted)

那只会改变地址。当我去找导师时,他告诉我应该说:

    icopy.data = newNode.data;

("data" 是提及我需要获取节点内的各个数据类型的快捷方式。)所以我写道:

    icopy.GetI() = newNode.GetI();  

当我执行此操作时遇到此错误:意外类型 required:variable、found:value。 我不知道该怎么做。任何帮助将不胜感激,我很乐意澄清任何事情。

*GetI:我的对象中的方法,可以访问每个节点中的整数值。
*p: 时间顺序的指针
*pi: 整数指针。
*fi: 整数链表前面

  public static void main(String args[])
  {
    String repeat = "y";
    boolean inserted = false;
    list1 fChr = null;
    list1 p = fChr;
    list1 icopy = null;
    list1 scopy = null;
    list1 dcopy = null;
    list1 fd = fChr;//front of the double list
    list1 fi = null;//front of the integer list
    list1 fStr = fChr;//front of the string list~
    while(repeat.equals("y"))//while the user agrees to adding a new node
    {
        if(fChr == null)// if the front is empty
        {
            fChr = new list1();//create a new node by calling object and sets it as the front
        }
        else
        {
            p = fChr;
            while(p.next != null)//finds the end of the Linked list
            {
                p = p.next;//moves the pointer p down the list
            }
            list1 newNode = new list1();
            icopy.GetI() = newNode.GetI();// make a copy of newNode
            p.next = nexNode;//put in chronological order
            while(p != null)
            {
                if(fi == null)
                {
                    fi = n;
                }
                else if(n.GetI() < fi.GetI)//check at beginning
                {
                    //put at beginning
                }                    

                else if(icopy.GetI() < p.next.GetI())//check in between nodes
                {
                    //put in between
                }
                //does it go at the end
            }
        }
        repeat = JOptionPane.showInputDialog("Would you like to add a node [y/n]");
    }
    PrintMenu(fChr, fi, fd, fStr);// sends the user to the menu screen
}

这里有几件事你不明白。首先,在 Java iCopy.getI() = ... 中没有任何意义。当一个方法 returns 一个值时,如果你想改变它,它需要被分配给一个变量。如果你想改变实例变量,你需要一个单独的方法,叫做 iCopy.setI().

听起来好像您并没有寻求排序方面的帮助,所以我的回答仅限于创建列表的副本。

您教授的意思是,确保数据在多个链表中保持一致的最简单方法是将存储数据的 class 与链表的节点分开。所以我希望你的 class 结构最终看起来像:

class Data {
    private final int intValue;
    private final String strValue;
    private final double doubleValue;
}

class Node {
    private final Data data;
    private Node next;

    public Node(Data data) {
        this.data = data;
        this.next = null;
    }
}

现在,如果你想创建一个新的链表,其数据与旧链表相同,那么你可以向 Node 添加一个构造函数,以创建对原始数据的引用:

class Node {
    public Node copy() {
        Node copy = new Node(data);
        if (next != null)
            copy.next = next.copy();
        return copy;
    }
}

希望您能看到它的作用:它创建一个新节点引用与该节点相同的数据,然后使用递归复制列表的其余部分。

现在创建每个排序顺序可能如下所示:

Node listByInt = list.copy();
/* code to sort listByInt according to data.intValue */

如果您还需要一些有关排序的提示,请添加评论,但我建议您在尝试之前先将代码设置为具有相同的列表副本。

最后一点,您不一定需要单独的链表来解决这个问题。另一种方法是将原始插入顺序存储在节点中。然后,您可以在打印列表之前按任何顺序(包括原始插入顺序)进行排序。就个人而言,我更喜欢将其作为解决方案,除非存在性能问题(例如,您需要多次使用每个排序列表)。