简体   繁体   English

递归地将新节点添加到 LinkedList 的末尾?

[英]Recursively adding a new node to the end of a LinkedList?

Having a lot of trouble trying to write a recursive method to add a new Node to the end of a LinkedList.尝试编写递归方法以将新节点添加到 LinkedList 的末尾时遇到很多麻烦。 I've been staring at this for a looooong time and still my terminal is printing blanks...if someone can help me out that would be greatly appreciated!我已经盯着这个看很久了,但我的终端仍然在打印空白……如果有人能帮助我,我将不胜感激! Still trying to wrap my head around the concept of recursion.仍然试图让我的头脑围绕递归的概念。 If I try to do the problem iteratively with a while loop, then I'm able to do it no problem but my assignment requires me to write it recursively.如果我尝试使用 while 循环迭代地解决问题,那么我可以毫无问题地做到这一点,但是我的作业要求我递归地编写它。 Thanks for the help :)谢谢您的帮助 :)

public class LinkedList
{
    //
    //Instance variable
    //
    private Node top;


    //
    //Instance and static methods below
    //

    //Accessor for the top Node
    public Node getTop()
    {
        return top;
    }

    public void add(Object data) {
        Node newNode = new Node(data,null);
        addRec(top,newNode);
}

    private Node addRec(Node start, Node newNode) {
        if (start == null) {
            start = newNode;
            return start;
        }
        start.setLink(addRec(start.getLink(), newNode));
        return start;
    }

    public String toString() {
        String value = "";
        Node current = top;
        while (current != null) {
            value += current.getData();
            current = current.getLink();
        }
        return value;
    }

Node class:节点类:

public class Node
{
    //
    //Instance variables
    //
    private Object data;
    private Node link;

    //
    //Constructor
    //
    public Node (Object initData, Node initLink)
    {
        data = initData;
        link = initLink;
    }

    //
    //Accessors
    //
    public Object getData()
    {
        return data;
    }

    public Node getLink()
    {
        return link;
    }

    public void setData(Object data) {
        this.data = data;
    }

    //
    //Mutators
    //
    public void setLink(Node newLink)
    {
        link = newLink;
    }
}

Recursively adding a node to the linked list is a fairly straightforward concept.向链表递归添加节点是一个相当简单的概念。 Lets look at the algorithm:让我们看一下算法:

1] If the given node does not have a node after it (which we will call the next node), we add the current node. 1] 如果给定的节点后面没有节点(我们称之为下一个节点),我们添加当前节点。

2] Otherwise, we add the node to the next node. 2] 否则,我们将该节点添加到下一个节点。 This would involve performing step 1 on the next node.这将涉及在下一个节点上执行步骤 1。

If you notice, Step 2 has a recursive call to step 1, as it is referencing the same node.如果您注意到,第 2 步会递归调用第 1 步,因为它引用了同一个节点。

To implement this, first, we create a child class called Node, inside the LinkedList.为了实现这一点,首先,我们在 LinkedList 中创建一个名为 Node 的子类。 This will require an int to store the data, and a Node that points to the next Node.这将需要一个 int 来存储数据,以及一个指向下一个 Node 的 Node。

we create the method add() inside Node.我们在 Node.js 中创建了 add() 方法。

To implement step 1, we check if next is equal to null.为了实现步骤 1,我们检查 next 是否等于 null。 If it is, we add the node as the next on on the linked list.如果是,我们将节点添加为链表上的下一个。

if(this.next== null)this.next= toAdd;

To implement step 2, we say otherwise, we call the method add on the next Node, and pass the value to add.要实现第 2 步,我们另说,我们在下一个 Node 上调用 add 方法,并将值传递给 add。

if(this.next== null)this.next= toAdd;

Now, we have to implement this in the class LinkedList.现在,我们必须在类 LinkedList 中实现它。

We declare a root node, where the list starts.我们声明一个根节点,列表开始的地方。

Now, we declare a method that will do the following:现在,我们声明一个将执行以下操作的方法:

add a value to root.给 root 添加一个值。

That's it.就是这样。

Recursion takes care of the rest.递归处理剩下的事情。

Think about it, if root has a node after it, the data will be added to the next node, and so on.想一想,如果root后面有一个节点,数据就会被添加到下一个节点,依此类推。

Therefore, problem sorted, you have your list!因此,问题排序,你有你的清单!

    public class LinkedList
{
    private Node root;

    private class Node{
        int data;
        Node next;
        public Node(int data){

            this.data = data;
            this.next = null;

        }

        public void add(Node toAdd){

            if(this.next== null)this.next= toAdd;
            else this.next.add(toAdd);

        }

    }

    public LinkedList(int root){

        this.root = new Node(root);
    }

    public void add(int toAdd){
        this.root.add(new Node(toAdd));
    }
}

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

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