理解线性链表
我在理解线性链表数据结构时遇到一些问题。这就是我定义列表元素的方式:
class Node{
Object data;
Node link;
public Node(Object pData, Node pLink){
this.data = pData;
this.link = pLink;
}
}
为了简单起见,我们说列表是链接的节点,因此我们不需要定义类列表(递归原理)。
我的问题是,我真的很困惑如何理解节点是如何连接的,更准确地说是连接节点时节点的顺序。
Node n1 = new Node(new Integer(2), null);
Node n2 = new Node(new Integer(1), n1);
什么是链接?它是上一个元素还是下一个元素?还有其他建议可以帮助我理解这个数据结构吗?
I have some problems understanding the linear linked list data structure. This is how I define a list element:
class Node{
Object data;
Node link;
public Node(Object pData, Node pLink){
this.data = pData;
this.link = pLink;
}
}
To keep it simple we say that a list are linked nodes so we do not need to define a class list (recursion principle).
My problem is that I am really confused in understanding how nodes are connected, more precisely the sequence of the nodes when we connect them.
Node n1 = new Node(new Integer(2), null);
Node n2 = new Node(new Integer(1), n1);
What is link? Is it the previous or the next element? Any other suggestions to help me understanding this data structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许这张图可以帮助你理解。
(请注意,箭头是引用,而不是 Java 的指针)
“列表”将是对第一个的引用节点。
Maybe this drawing will help you to understand.
(Be aware that arrows are references NOT pointers for Java)
The "list" will be a reference to the very first node.
link
是对列表中下一个节点的引用。因此,您将从列表中的第一个节点
n1
开始,您可以直接引用该节点。要获取列表中的第二个节点,您需要引用n1.link
。要迭代列表,您必须有一些起点,例如
n1
,然后重复引用link
:link
is a reference to the next Node in the list.So you would start at the first node in the list,
n1
, which you'd have a direct reference to. To get the second node in the list, you'd referencen1.link
.To iterate over the list, you would have to have some starting point, such as
n1
, then repeatedly referencelink
:在单链表中,它是“下一个”。
它看起来像 Java,即使您没有这样标记它。如果这是真的,请考虑使用泛型:
In a singly-linked list, it's "next".
It looks like Java, even though you haven't tagged it as such. If that's true, consider using generics:
我有两个建议。
首先,关于“这是上一个元素还是下一个元素”:这取决于您的数据结构。通常它是下一个元素。
其次,我建议使用指针或引用。 (并且您的 C++ 语法不正确:
this
是一个指针,并且new
运算符也返回一个指针。但不确定您是否使用 C++,因为您没有指定一种语言。)例如:
这将是一个更有效的结构。然后你可以这样做:
或者简单地
然后你可以按如下方式迭代列表:
我希望这有帮助!
如果你使用现代语言,C++ 的 stl 中已经有预制的链表结构,.NET 中的 System.Collections.Generic 中已经有预制的链表结构,我确信也有一个 Java 对应的结构。
I have two suggestions.
First, about the "is this the previous or the next element": it depends on your data structure. Usually it is the next element.
Second, I'd recommend using a pointer or a reference. (And your C++ syntax is incorrect:
this
is a pointer, and thenew
operator also returns a pointer. Not sure if you use C++ though, since you didn't specify a lanugage.)So for example:
This would be a more valid structure. Then you could do:
or simply
Then you could iterate through the list as follows:
I hope this helps!
If you use a modern language, there are already premade linked list structures in C++'s stl, in .NET it's in System.Collections.Generic and I'm sure there is also a Java counterpart.