简体   繁体   English

如何在链表中创建根节点?

[英]How to create a root node in a linked list?

I'm trying to learn linked-lists in Java and had some questions about the code below:我正在尝试学习 Java 中的链表,并对以下代码有一些疑问:

public class List {
    Node root;
    public List() {
        // constructor
    }

    public int pop() {
        // pop logic
    }

    public int push(int data) {
        // push logic
    }
}

I'd like to have a List class for popping and pushing data into the linked list.我想要一个列表 class,用于将数据弹出和推送到链表中。 However, since the list won't have any default data on instantiation, what would be the best way for storing a reference to the root node?但是,由于列表在实例化时没有任何默认数据,存储对根节点的引用的最佳方式是什么?

In C, I would just have a pointer like:在 C 中,我只会有一个像这样的指针:

Node * root;

But since Java does not have pointer, would having a simple declaration like:但是由于 Java 没有指针,所以有一个简单的声明如下:

Node root;

... be acceptable? ……可以接受吗? I haven't used Java in a while, but doesn't allocating memory to an object declared as a class variable cause potential memory issues?我有一段时间没有使用 Java,但是没有将 memory 分配给声明为 class 变量的 object 会导致潜在的 memory 问题吗? Thanks!谢谢!

Yes, a simple declaration like Node root is acceptable.是的,像Node root这样的简单声明是可以接受的。 It is not actually a pointer, but a reference that can potentially refer to any Node .它实际上不是一个指针,而是一个可以引用任何Node的引用。

References in Java are conceptually equivalent to C pointers, but are less flexible and use a simpler syntax. Java 中的引用在概念上等同于 C 指针,但灵活性较低且使用更简单的语法。

Yes,是的,

Node root;

Is acceptable.是可以接受的。 Every non-primitive object (including arrays of primitives or objects) in Java is actually a reference to an object, so it is like a C pointer in many ways. Java 中的每个非原始 object(包括原始或对象的 arrays)实际上是对 object 的引用,因此它在许多方面就像一个 C 指针。

It is actually so much like a pointer, that this declaration by itself isn't actually creating an object. It is a reference that is not pointing to anything yet, and if you try to use root before assigning it to a new Node() first, you will get a NullPointerException .它实际上非常像一个指针,这个声明本身实际上并没有创建一个 object。它是一个还没有指向任何东西的引用,如果你在将它分配给一个new Node()之前尝试使用root首先,你会得到一个NullPointerException

Yes, Node root;是的, Node root; is absolutely fine.绝对没问题。 Just make sure you don't change the value of root .只要确保您不更改root的值即可。 For using it, create another variable, for traversing a path: Node start = root;为了使用它,创建另一个变量,用于遍历路径: Node start = root; This way root remains untouched.这样root保持不变。

I haven't used Java in a while, but doesn't allocating memory to an object declared as a class variable cause potential memory issues?

No it doesn't.不,它没有。 While simply writing Node root;而简单的写Node root; doesn't allocate any memory, root = new Node();不分配任何 memory, root = new Node(); does.做。 Take a note here that class members in java are static, the non-static members are global variables .这里注意一下,java中的class members为static,非静态成员为global variables Allocating memory to global variables in java is a common practice.将memory分配给java中的全局变量是一种常见的做法。 For example, the variable where you actually store the list, will be a global variable, and you will have to allocate memory to it.例如,您实际存储列表的变量是一个全局变量,您必须为其分配 memory。

Java has a robust memory management system, so you won't run into memory issues too easily. Java 拥有强大的 memory 管理系统,因此您不会轻易遇到 memory 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM