理解节点'link'中的节点Class

Understanding the Node 'link' in the Node Class

因此在节点 Class 中,节点对象属性之一是 link,它本身就是一个节点。 link 节点在某种程度上是对象本身吗?为了真正解释我的问题,假设我们有一个 LinkedList:

temp1 是列表中的第一个节点,temp2 是列表中的第二个节点。

实际上,temp1temp2只是堆中真实对象的引用变量。所以当我做 temp1.link (temp1.link = temp2) 时,这是否意味着 temp1.link 作为一个整体等于 temp2 还是仅意味着 temp1.link.link 部分=] 等于 temp2?

我很困惑,因为 link 是一个节点,这是否意味着它本身就是一个对象的引用变量?一个对象可以有一个也是相同数据类型的属性吗(比如自引用)?

是否link指针,指向内存堆中的temp2对象地址。或者 link 指针首先指向对象引用变量 temp2,然后 'temp2' 指向内存中的对象地址,然后 returns 向上指向 link?

首先开始这个问题的原因是: 与 newTreeNode.data.compareTo(current.data) 相比,newTreeNode.compareTo(current)//TreeNode current = root 做了什么,因为不仅对象引用变量 newTreeNode 可以使用 compareTo() 方法而不是 newTreeNode.data

Is the link Node an object itself in a way?

它指向 Node 的方式与 temp1temp2 相同。但是实际上并没有所有权的概念。 link 不是一个节点,但它可以告诉你在哪里可以找到一个。您可以将 link 视为内存中的一个索引。它通过充当指向链中下一个节点的指针将该节点链接到下一个节点。 link 可以是 null,相同的 Node(尽管这对于链表来说意义不大),或者不同的 Node.

SO when I do temp1.link = temp2 does that mean temp1.link as an entirety equal temp2 or does that mean just the .link portion of temp1.link equal temp2?

表示temp1.linktemp2是同一个指针。从概念上讲,您只能使用 Java 中的引用,因为您实际上无法在堆栈上分配对象。

public class Node {
    int value;
    Node link;
}


// a and b point to the data of each node
Node a = new Node();
Node b = new Node();

// Give some data
a.value = 2;
b.value = 9;

// Now a contains a pointer to the data of b.
a.link = b;

// We can check this by printing the value of the linked node
System.out.println(a.value); // 2
System.out.println(a.link.value); // 9

// We can also have an object store a pointer to itself
b.link = b;

// We can check this by printing the value of the linked node
System.out.println(b.link.value);

// We can also check b and b.link are the same pointers.
System.out.println(b == b.link); // true

I am confused because since link is a Node does that mean it is a reference variable to an object on its own? Can an object have an attribute that is also the same datatype(like self-referential)?

不要将 link 视为 Nodelink 只是指向类型 Node 的指针。因为只是一个指针,所以可以是self-referential.

Does the link pointer, point to temp2 objects address in memory heap. Or does the link pointer point to the object reference variable temp2 FIRST and then temp2 points to the object address in memory and returns it up the line to link?

linktemp2都指向同一个Node堆中的数据。指针只是整数,所以 linktemp2 在内存中完全无法区分。 Java 不允许您使用原始指针,因此无法获取指向堆栈上变量的指针。