英文:
What value does next have for each node in a linked list?
问题
我需要为链表栈编写一个push方法。栈应该使用一个特定的节点类。这个特定的类有两个构造函数的输入变量,其中一个是我需要推送的值,另一个是指向下一个节点的下一个引用:
public class StackNode<T> {
protected T data;
protected StackNode<T> next;
public StackNode(T v, StackNode<T> n) {
data = v;
next = n;
}
}
如果我想要将我的第一个节点添加到索引0的列表中,如果"next"指向下一个节点,它应该采用什么值?是1吗?
StackNode<ElementType> linkedList = new StackNode<ElementType>(value, 1);
英文:
I have to write a push method for a linked list stack. The stack should use a specific class for the nodes. This specific class has two input variables for the constructor one of which is the value that I need to push and the other one is a next reference to the next node:
public class StackNode<T> {
protected T data;
protected StackNode<T> next;
public StackNode(T v, StackNode<T> n)
{
data = v;
next = n;
}
}
If I want to add my first node to the list at index 0, what value should "next" take if it points to the next node? Would it be 1?
StackNode<ElementType> linkedList=new StackNode<ElementType>(value,1);
</details>
# 答案1
**得分**: 0
`next`应该指向列表中现有的内容:
```java
StackNode<ElementType> linkedList = new StackNode<>(value, existingList);
其中,existingList
是类型为StackNode<ElementType>
的变量。
它可能被定义为:
StackNode<ElementType> existingList = null; // 无节点的空列表
英文:
next
should point to the existing contents of the list:
StackNode<ElementType> linkedList = new StackNode<>(value, existingList);
where existingList is a variable of type StackNode<ElementType>
It might be defined as:
StackNode<ElementType> existingList = null; // empty list without nodes
答案2
得分: 0
如果您想创建一个仅包含一个节点的列表,那么传递 null
new StackNode<ElementType>(value, null);
否则,您需要有另一个具有值的节点
new StackNode<ElementType>(value, new StackNode<ElementType>(new ElementType<Integer>(1), null));
我建议创建一个类似于 new StackNode<>()
的终端节点,然后在该类上实现一个 equals
方法。
英文:
If you want to make a list of one node, then pass null
new StackNode<ElementType>(value, null);
Otherwise, you need to have another node with a value
new StackNode<ElementType>(value, new StackNode<ElementType>(new ElementType<Integer>(1), null));
I would suggest creating a terminal node like new StackNode<>(null, null)
, then implementing an equals
method on that class
答案3
得分: 0
这取决于实际堆栈的实现方式。如果它是一个“真正的”链表,每个节点都应该保存下一个节点,并且应该是相同的StackNode
类型。问题是,那个初始的第一个节点的值是什么?将其存储为null
值可能是最合理的,因为唯一的其他选择是使用一些特殊的EndStackNode
类,这只会产生一些几乎无用的样板代码。只需确保在遇到null
时要小心,并且在迭代堆栈时进行适当的空值检查。
为了解决这个问题,你可能想要查阅 java.util.Optional
,它对于处理这种情况非常有用。
import java.util.Optional;
public class StackNode<T> {
protected T data;
protected Optional<StackNode<T>> next;
public StackNode(Y v) {
this(v, Optional.empty());
}
public StackNode(T v, Optional<StackNode<T>> n) {
data = v;
next = n;
}
}
英文:
It depends on how the actual stack is implemented. If its a "true" linked list, each node should hold the next node and should be of the same StackNode
type. The issue is then what is the value of that initial first node? It would make a the most sense to store that as a null
value, since the only other option is to have some special EndStackNode
class which would just be some almost useless boilerplate. Just make sure that you are careful around that null, and are putting the proper null checks whenever you are iterating over the stack.
To get around this you might want to look into java.util.Optional
which is super useful for just that sort of thing.
import java.util.Optional;
public class StackNode<T> {
protected T data;
protected Optional<StackNode<T>> next;
public StackNode(Y v) {
this(v, Optional.empty());
}
public StackNode(T v, Optional<StackNode<T> n>)
{
data = v;
next = n;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论