理解线性链表

发布于 2024-10-10 03:08:26 字数 474 浏览 1 评论 0原文

我在理解线性链表数据结构时遇到一些问题。这就是我定义列表元素的方式:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红衣飘飘貌似仙 2024-10-17 03:08:26

也许这张图可以帮助你理解。

alt text

(请注意,箭头是引用,而不是 Java 的指针)

“列表”将是对第一个的引用节点。

Maybe this drawing will help you to understand.

alt text

(Be aware that arrows are references NOT pointers for Java)

The "list" will be a reference to the very first node.

小梨窩很甜 2024-10-17 03:08:26

link 是对列表中下一个节点的引用。

因此,您将从列表中的第一个节点 n1 开始,您可以直接引用该节点。要获取列表中的第二个节点,您需要引用n1.link

要迭代列表,您必须有一些起点,例如 n1,然后重复引用 link

Node n = n1;
while (n != null) {
    println(n.data);
    n = n.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 reference n1.link.

To iterate over the list, you would have to have some starting point, such as n1, then repeatedly reference link:

Node n = n1;
while (n != null) {
    println(n.data);
    n = n.link;
}
情丝乱 2024-10-17 03:08:26

在单链表中,它是“下一个”。

它看起来像 Java,即使您没有这样标记它。如果这是真的,请考虑使用泛型:

public class Node<T>
{
    T value;
    Node<T> next;
}

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:

public class Node<T>
{
    T value;
    Node<T> next;
}
笑看君怀她人 2024-10-17 03:08:26

我有两个建议。

首先,关于“这是上一个元素还是下一个元素”:这取决于您的数据结构。通常它是下一个元素。

其次,我建议使用指针或引用。 (并且您的 C++ 语法不正确:this 是一个指针,并且 new 运算符也返回一个指针。但不确定您是否使用 C++,因为您没有指定一种语言。)

例如:

class Node {
    Object data;
public:
    Node *next;

    Node (Object pData, Node *pLink) {
        this->data = pData;
        this->next = pLink;
    }
}

这将是一个更有效的结构。然后你可以这样做:

Node *n3 = new Node(new Integer(2), null);
Node *n2 = new Node(new Integer(1), n1);
Node *n1 = new Node(new Integer(3), n2);

或者简单地

Node *n1 = new Node(new Integer(3), new Node(new Integer(1), new Node(new Integer(2), NULL)));

然后你可以按如下方式迭代列表:

for (Node *current = n1; current != NULL; current = current->next)
{
    // do something with the current element
}

我希望这有帮助!


如果你使用现代语言,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 the new operator also returns a pointer. Not sure if you use C++ though, since you didn't specify a lanugage.)

So for example:

class Node {
    Object data;
public:
    Node *next;

    Node (Object pData, Node *pLink) {
        this->data = pData;
        this->next = pLink;
    }
}

This would be a more valid structure. Then you could do:

Node *n3 = new Node(new Integer(2), null);
Node *n2 = new Node(new Integer(1), n1);
Node *n1 = new Node(new Integer(3), n2);

or simply

Node *n1 = new Node(new Integer(3), new Node(new Integer(1), new Node(new Integer(2), NULL)));

Then you could iterate through the list as follows:

for (Node *current = n1; current != NULL; current = current->next)
{
    // do something with the current element
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文