Why do we have to create a temp object named start in a linked list when we have initially created a const object named next in the Node class?
Here is my linked list code:
public class Node {
// here we have created "next" as an object of class Node
// to make the node class as a reference class
private int data;
private Node next; // reference object
//Constructor
public Node(int data) {
this.data = data;
}
public static void main(String[] args) {
Node start = new Node(22); //temp reference
// Why do we have to create another object here "start"?
start.next = new Node(33);
}
}
CodePudding user response:
if you mean why you need to make a new Node to add the next object and why you can't just give a value to the next object like start.next = 5, it's because the .next object is of type Node which doesn't match the Integer type of 5.
what you can do, is to make a .next() fuction for the Node class.
this could be done like below:
class Node {
private int data;
private Node next; // reference object
//Constructor
public Node(int data) {
this.data = data;
public next(int value) {
this.node = new Node(value);
}
}
and now you can do this to add the next value to your linked list:
public static void main(String[] args) {
Node start = new Node(22);
start.next(32);
}
CodePudding user response:
Why we have to create temp object named
startin the linked list when we have initially created aconstobject namednextin theNodeclass?
It seems the static main function is creating confusion here: it is a static method that runs when there is no Node instance yet. This function starts with nothing.
It creates a first node instance with new Node(22). Obviously that new node reference needs to be maintained somehow, otherwise it will be lost. That is why it is assigned to a local variable start. This is not really a "temporary" variable. It is the first (and so far: the only) node of a list.
The next property of that node instance will initially be null, which means there is no second node yet in that list. You can picture it as follows:
start
↓
┌────────────┐
│ data: 22 │
│ next: null │
└────────────┘
The program then continues to create a second node, with new Node(33):
start returned from new Node(22)
↓ ↓
┌────────────┐ ┌────────────┐
│ data: 22 │ │ data: 33 │
│ next: null │ │ next: null │
└────────────┘ └────────────┘
Again that new node reference needs to be stored somewhere. As we apparently want to create a list with two nodes, we should append this new node to the one we created earlier. And so we assign that to start.next. This results in this situation:
start
↓
┌────────────┐ ┌────────────┐
│ data: 22 │ │ data: 22 │
│ next: ────────> │ next: null │
└────────────┘ └────────────┘
But you still need start, otherwise these node instances are lost. So start is not temporary at all. It is the reference we need to access the first node in the linked list, and from there we can access the nodes the follow it, using the next reference.
The static main function is not really related with the Node class, and I think that brought confusion. It is a method that would normally be placed in another class. It runs when there is no instance of Nodeyet. It needs to create node instances to build a linked list. The next property serves to link individual nodes together, but the very first node reference will need to be stored in some variable. That is the role of start.
