递归链接集合添加方法

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

RecursiveLinkedCollections add methods

问题

以下是翻译好的代码部分:

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

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

在代码的 return recAdd(front, data); 行中,您遇到了类型错误,因为 recAdd 方法返回的是 LLNode<T> 类型而不是 boolean 类型。您的 add 方法的签名要求返回一个 boolean 值,以指示添加是否成功。

要解决这个问题,您可以修改 recAdd 方法,使其返回一个 boolean 值,以反映添加操作是否成功。这是修改后的代码:

private boolean recAdd(LLNode<T> node, T data) {
    if (node.getLink() == null) {
        node.setLink(new LLNode<T>(data));
        return true; // Return true to indicate successful addition
    }
    return recAdd(node.getLink(), data);
}

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

这样,recAdd 方法现在返回一个布尔值,指示是否成功添加节点,而 add 方法将这个布尔值传递给调用者,以满足接口要求。

英文:

for one of my homework problems we are supposed to recursively add a node to a linked list. here is my code:

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

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

As you can see, I am supposed to call the recAdd method from the add function, but there is an error on the

   return recAdd(front, data);

line, as I can not convert LLNode<T> to boolean. Are there any suggestions? for the recAdd method, I am supposed to return a node, but I can not find a way to work around this. Also, here is a descirption of what the add method does in the CollectionInterface:

  	/**
 * Attempts to add element to this collection.
 * @param element
 * @return Returns true if successful, false otherwise. 
 * Return false if element is null, otherwise true.
 */
boolean add(T data); 

答案1

得分: 1

你可以始终在add操作中返回true,如下所示:

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

正如你可以在LinkedList实现ArrayList实现中看到的那样,Java已经这样做了。

英文:

You could always return true in the add operation, like

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

As you can see in the LinkedList implementation and the ArrayList implementation, Java already does that.

答案2

得分: 0

你的问题没有明确说明在 add 方法中返回 boolean 的目的是什么,所以我认为除非你提供更多细节,否则无法回答这个问题。

根据 Java Collection 接口的约定,CollectionList 应该在 add 方法中返回 boolean。然而,由于你的代码没有处理 false 的情况,为什么不像 Java 中的原生实现一样返回 void 或始终返回 true 呢?

如果你仍然想要返回 boolean 值,在你的代码片段的基础上有一些解决方法,比如创建一个表示 add 状态的字段标志,或者将要添加的对象与尾部进行比较。请注意,这些方法可能会依赖于预设条件(例如:无重复值等)。

英文:

Your question doesn't clarify what the purpose of returning boolean in add is, so I believe this question can't be answered unless you give us more detail.

As per the convention of Java Collection interface, Collection and List should return boolean in add method. However, since your code doesn't handle false cases, why don't you return void or always true like native implementation in Java?

In case you still want to return boolean value, based or your code snippet, there are some work around like creating a field-flag indicating add status of add, or comparing the tail with the object you want to add. Note that these may not work depended on pre-condition (i.e: no duplicated values, etc).

答案3

得分: 0

必须是

    private LLNode<T> recAdd(LLNode<T> node, T data) {
        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;
    }
英文:

Must be

private LLNode&lt;T&gt; recAdd(LLNode&lt;T&gt; node, T data) {
    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;
}

huangapple
  • 本文由 发表于 2020年9月7日 01:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767292.html
匿名

发表评论

匿名网友

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

确定