Home > Net >  Understanding the Node 'link' in the Node Class
Understanding the Node 'link' in the Node Class

Time:03-23

So in the Node Class one of the Node objects attributes is link which itself a Node. Is the link Node an object itself in a way? To truly explain my question lets say we have a LinkedList with:

temp1 being the 1st Node in the list and temp2 being the 2nd Node in the list.

In reality temp1 and temp2 are just reference variables to the real objects in the heap. SO when I do temp1.link (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? I am confused because since link is a Node does that mean it is a refernce variable to an object on its own? Can an object have an attribute that is also the same datatype(like self-referential)?

MY FINAL QUESTION IS: 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?

If I need to explain anything better, so that you can answer me better please let me know!

What Started this question in the first place was: What does newTreeNode.compareTo(current)//TreeNode current = root do compared to newTreeNode.data.compareTo(current.data) because doesnt only the object reference varaible newTreeNode have access to using the compareTo() method and not newTreeNode.data.

CodePudding user response:

Is the link Node an object itself in a way?

It points to a Node the same way temp1 or temp2 does. There is not really a concept of ownership though. link is not a node, but it can tell you where to find one. You can think of link as just an index in memory. It links that node to the next one by acting as a pointer to the next node in the chain. link could be null, the same Node (although that would not make much sense for a linked list), or a different 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?

It means that temp1.link is the same pointer as temp2. Conceptually you only ever work with references in Java since you can't actually allocate an object on the stack.

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)?

Don't think of link as a Node. link is just a pointer to something of type Node. Because it is just a pointer, it can be 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?

Both link and temp2 point to the data in the heap for the same Node. Pointers are just integers so link and temp2 are completely indistinguishable in memory. Java does not let you use raw pointers so it is not possible to take a pointer to a variable on the stack.

  • Related