简体   繁体   中英

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. 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. 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.

2] Otherwise, we add the node to the next node. This would involve performing step 1 on the next node.

If you notice, Step 2 has a recursive call to step 1, as it is referencing the same node.

To implement this, first, we create a child class called Node, inside the LinkedList. This will require an int to store the data, and a Node that points to the next Node.

we create the method add() inside Node.

To implement step 1, we check if next is equal to 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.

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

Now, we have to implement this in the class 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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