我正确实现了递归加法函数吗?我的前置节点仍然为空。

huangapple go评论74阅读模式
英文:

Did i correctly implement the recursive add function? My front node is remaining null

问题

public class RecursiveLinkedCollection<T> implements CollectionInterface<T> {
    
    LLNode<T> front;
    int size = 0;
    
    RecursiveLinkedCollection() {
        front = null;
        size = 0;
    }

    private LLNode<T> recAdd(LLNode<T> node, T data) {
        
        if(node == null) {
            LLNode<T> newNode = new LLNode<T>(data);
            node = newNode;
        }
        if(node.getLink() == null) {
            LLNode<T> newNode = new LLNode<T>(data);
            node.setLink(newNode);
            return newNode;
        }
        return recAdd(node.getLink(), data);
    }

    @Override
    public boolean add(T data) {
        recAdd(front, data);
        return true;
    }
}
英文:

I am supposed to implement a recursive linked list, but after writing the code and debugging, it seems that my front node is remaining unchanged (it is staying at null). Any help will be appreciated.

public class RecursiveLinkedCollection&lt;T&gt; implements CollectionInterface&lt;T&gt; {
    
    LLNode&lt;T&gt; front;
    int size = 0;
    
    RecursiveLinkedCollection() {
        front = null;
        size = 0;
        
    }

    private LLNode&lt;T&gt; recAdd(LLNode&lt;T&gt; node, T data) {
        
        if(node == null) {
            LLNode&lt;T&gt; newNode = new LLNode&lt;T&gt;(data);
            node = newNode;
        }
        if(node.getLink() == null) {
            LLNode&lt;T&gt; newNode = new LLNode&lt;T&gt;(data);
            node.setLink(newNode);
            return newNode;
        }
        return recAdd(node.getLink(), data);
    }

    @Override
    public boolean add(T data) {
        recAdd(front, data);
        return true;
    }
}

答案1

得分: 0

根据您的add方法,您试图从“front”节点添加新节点。在您的递归中,如果检查node为null,则只有当“front”未设置时才会发生这种情况。您试图设置node = newNode,但由于java始终是按值传递的,因此对front的引用永远不会被设置。

public class RecursiveLinkedCollection<T> implements CollectionInterface<T> {
    
    LLNode<T> head;
    int size;
    
    RecursiveLinkedCollection() {
    }

    private LLNode<T> recToTail(LLNode<T> next, T data) {
        LLNode<T> newNode = new LLNode<T>(data);
        if(next == null) {
            head = newNode;
            size++;
            return head;
        }
        if(next.getLink() == null) {
            next.setLink(newNode);
            size++;
            return newNode;
        }
        return recAdd(next.getLink(), data);
    }

    @Override
    public boolean add(T data) {
        return recAdd(head, data) != null;
    }
}
英文:

According to your add method, you try to append the new node from the front-node. In your recursion if you check for node being null, this can only happen if front is not set. You try to set node = newNode, but because java is always pass-by-value the reference to front is never set.

public class RecursiveLinkedCollection&lt;T&gt; implements CollectionInterface&lt;T&gt; {
    
    LLNode&lt;T&gt; head;
    int size;
    
    RecursiveLinkedCollection() {
    }

    private LLNode&lt;T&gt; recToTail(LLNode&lt;T&gt; next, T data) {
        LLNode&lt;T&gt; newNode = new LLNode&lt;T&gt;(data);
        if(next == null) {
            head = newNode;
            size++;
            return head;
        }
        if(next.getLink() == null) {
            next.setLink(newNode);
            size++;
            return newNode;
        }
        return recAdd(next.getLink(), data);
    }

    @Override
    public boolean add(T data) {
        return recAdd(head, data) != null;
    }
}

huangapple
  • 本文由 发表于 2020年9月8日 03:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63783376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定