简体   繁体   English

在链表Java中复制节点

[英]duplicating a node in a linked list java

just to start off, this is homework and thank you for your assistance ahead of time. 只是开始,这是家庭作业,感谢您提前提供的帮助。 I keep getting stuck on little problems so I am hoping you guys can help me with one. 我一直在解决小问题,所以希望你们能帮我一个忙。 What I am trying to do is create a linked list that has multiples functions. 我正在尝试做的是创建一个具有多重功能的链表。 The one I am having trouble with is sorting(I can do the other ones). 我遇到麻烦的一个是排序(我可以做其他的)。 Each node holds a string, an integer and a double. 每个节点包含一个字符串,一个整数和一个双精度型。 I need to be able to sort by each of these and by the order it was inputted, on the user's request. 我需要能够根据用户的要求,按输入顺序和输入顺序进行排序。 ***It is also important to mention that the variables in my object are private and my object is called list1. ***同样重要的是要提到我的对象中的变量是私有变量,而我的对象称为list1。 Basically, I have to make one linked list for the chronological order and one for each other order. 基本上,我必须为时间顺序创建一个链接列表,并为彼此顺序创建一个链接列表。

My plan is to insert the nodes in their correct order as the user inputs them. 我的计划是在用户输入节点时以正确的顺序插入节点。 So as the user inputs a node, that node needs to go in the correct place in the chronological list and in the other lists. 因此,当用户输入一个节点时,该节点需要进入按时间顺序排列的列表和其他列表中的正确位置。 So, I need to copy the node to do this. 因此,我需要复制节点才能执行此操作。 However, I cannot simply just say 但是,我不能简单地说

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

That only changes the address. 那只会改变地址。 When I went to my instructor he told me that I should say: 当我去找老师时,他告诉我应该说:

    icopy.data = newNode.data;

("data" being the shortcut way of mentioning that I need to get the individual data types within the node.) So I wrote: (“数据”是提到我需要获取节点内各个数据类型的捷径。)因此,我写道:

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

When I do this I encounter this error: unexpected type required:variable, found:value. 当我这样做时,我遇到以下错误: 意外类型为必需:变量,找到:值。 I am not sure what to do. 我不确定该怎么办。 Any assistance would be appreciated and I would be happy to clarify anything. 任何帮助将不胜感激,我将很乐意澄清任何事情。

*GetI: method in my object that gives access to the integer value in each node. * GetI:对象中的方法,该方法可访问每个节点中的整数值。
*p: pointer for the Chronological * p:年表指针
*pi: pointer for the integer. * pi:整数的指针。
*fi: front of the integer linked list * 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
}

There are a few things here that you are not understanding. 这里有些事情您不了解。 Firstly, in Java iCopy.getI() = ... makes no sense. 首先,在Java中, iCopy.getI() = ...毫无意义。 When a method returns a value it needs to be assigned to a variable if you wish to change it. 当方法返回值时,如果您想更改它,则需要将其分配给变量。 If you want to change the instance variable you need a separate method called something like iCopy.setI() . 如果要更改实例变量,则需要一个名为iCopy.setI()的单独方法。

It sounds as though you're not asking for help with the sorting so I'll restrict my answer to creating copies of the list. 听起来好像您不是在寻求排序方面的帮助,所以我将回答仅限于创建列表的副本。

What your professor is getting at is that the easiest way to ensure the data is consistent in your several linked lists is to separate the class storing the data from the nodes of the list. 教授所要解决的问题是,确保多个链接列表中的数据一致的最简单方法是将存储数据的类与列表中的节点分开。 So I would expect your class structure to end up looking something like: 因此,我希望您的类结构最终看起来像:

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;
    }
}

Now if you want to create a new linked list with the same data as the old one then you can add a constructor to Node that creates a reference to the original data: 现在,如果您要创建一个与旧列表具有相同数据的新链表,则可以向Node添加一个构造函数,以创建对原始数据的引用:

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

Hopefully you can see what that does: it creates a new node referencing the same data as this one and then uses recursion to copy the rest of the list. 希望您能看到它的作用:它创建了一个新节点,该节点引用与此数据相同的数据,然后使用递归方法复制列表的其余部分。

Now creating each of the sort orders could look like: 现在,创建每个排序顺序如下所示:

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

Add a comment if you want some hints on sorting as well but I suggest you get your code to the point of having equal copies of lists before attempting that. 如果您还希望获得一些有关排序的提示,请添加一条注释,但是我建议您在尝试进行编码之前,先使代码具有相同的列表副本。

As a final note, you don't necessarily need to have separate linked lists to solve this problem. 最后一点,您不一定需要单独的链接列表来解决此问题。 An alternative would be to store the original insertion order in the node. 一种替代方法是将原始插入顺序存储在节点中。 You could then sort by any order (including original insertion order) before printing the list. 然后,您可以在打印列表之前按任何顺序(包括原始插入顺序)进行排序。 Personally I'd prefer that as a solution unless there are performance issues (eg you need to use each sorted list many times). 我个人更希望将其作为解决方案,除非存在性能问题(例如,您需要多次使用每个排序列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM