每个链表节点的”next”属性代表了指向下一个节点的引用。

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

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&lt;T&gt; {
	protected T data;
	protected StackNode&lt;T&gt; next;
	
	public StackNode(T v, StackNode&lt;T&gt; 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&lt;ElementType&gt; linkedList=new StackNode&lt;ElementType&gt;(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&lt;ElementType&gt; linkedList = new StackNode&lt;&gt;(value, existingList);

where existingList is a variable of type StackNode&lt;ElementType&gt;

It might be defined as:

StackNode&lt;ElementType&gt; 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&lt;ElementType&gt;(value, null);

Otherwise, you need to have another node with a value

new StackNode&lt;ElementType&gt;(value, new StackNode&lt;ElementType&gt;(new ElementType&lt;Integer&gt;(1), null));

I would suggest creating a terminal node like new StackNode&lt;&gt;(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&lt;T&gt; {
    protected T data;
    protected Optional&lt;StackNode&lt;T&gt;&gt; next;
    
    public StackNode(Y v) {
        this(v, Optional.empty());
    }

    public StackNode(T v, Optional&lt;StackNode&lt;T&gt; n&gt;)
    {
        data = v;
        next = n;
    }
}

huangapple
  • 本文由 发表于 2020年8月21日 05:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63513199.html
匿名

发表评论

匿名网友

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

确定