Add to linked list

I was wondering how the while loop executes. so how do we set 'next' to null when we first declare it when it changes to non null? and also what is Node n = this; ' I mean? Is it significant for this code? Whenever we declare a new instance of a Node object, does it make a copy of its individual fields from the class? Thank you very much! I would definitely appreciate a clear and understandable explanation. Thanks again =)

class Node { Node next = null; int data; public Node(int d) { data = d; } void appendToTail(int d) { Node end = new Node(d); Node n = this; while (n.next != null) { n = n.next; } n.next = end; } } 
+6
source share
3 answers

So, you have a class called Node with two instance variables called next and data. They are called instance variables, because they apply to instances of this class, and not to the class itself. That is, your class is basically a template (or plan) for objects, each of which will have its own data value and the next value.

To create an instance of the Node class, you need to call the constructor and pass the necessary parameters. In your case, the constructor:

  public Node(int d) { data = d; } 

To call this constructor, you use a new keyword (in Java, which I assume), for example:

  Node x = new Node(10); 

And note that you must specify an integer value for the constructor. In the body of the constructor (between {}), you see that the variable data is assigned a value in d, which is the value that you pass to the constructor, in this example, the value is 10. Now you have an object of type Node with a value of 10 as the data and zero of the following Node.

On this object, you can now call the appendToTail () method. Let's say we do this:

  x.appendToTail(20); 

Allows you to track what is happening.

  Node end = new Node(d); 

A new end is created with the name end Node, and we set the value to 20 for the data (remember that d has the value 20, because this is the value that we passed when the method was called). This is a completely independent node from x with its own unique value for data.

  Node n = this; 

This is the self-esteem of the current property. Since we will call this method x, this is the same object as x.

  while (n.next != null) { n = n.next; } 

This while loop will start looking for the end of the list, moving from the current Node to the next Node, until the next Node is zero. Since the only Node we have created so far is x, then n.next is actually null, so the while loop is not executed this time.

  n.next = end; 

Now we set the next value of n (which is x) to the end of the Node that was created. Now you have a list:

  10 -> 20 -> null 

Suppose you have to make the following call:

  x.appendToTail(30); 

Then a similar thing happens, except when you get into the while loop, the value of n.next is not zero, so you go to the body of the loop and assign n to point to n.next, which in our example is Node with 20. Next iteration of the loop will give a null value, so the loop will end, and a new Node with data 30 will be set to the next value of the last Node in the list. So you will have:

  10 -> 20 -> 30 -> null 
+6
source

To answer your questions:

Q: "since we set 'next' to null when we first declare it, when it changes to non null?"

When you have only one item in the list, the β€œnext” value of this Node will be set to NULL.

Q: "as well as what means Node n = this; 'mean?"

This instruction means that the reference variable 'n' accepts a reference to the current object that is specified by 'this'.

Q: "When we declare a new instance of the Node object, does it make a copy of its individual fields from the class?"

Instance instances will be created for each individual class in which you create the instance. This means that each Node will have a β€œnext” and a β€œdata”.

So, in your creation process, you will probably have something like this:

enter image description here

In addition, the while loop iterates to the end of the list and adds the item after the last Node to the list.

Hope this helps (: If you have any questions, send a message (:

+6
source

No node copies itself. The point of a linked list is for node to refer to the next line. Therefore, if you have 3 elements in a linked list, the first node has a link to the second, and the second to the third.

 Node one = new Node(1); one.appendToTail(2); 

will lead to node one by creating a new node and placing it in the next field one.next.data will be 2.

 one.appendToTail(3) 

will result in a node related to node 2, and node 2 will create node 3 and set it as the next field.

 one.data == 1 one.next.data == 2 one.next.next.data == 3 

The loop is mainly designed to find the last line of a node in a line (because it has the next set equal to null).

+1
source

Source: https://habr.com/ru/post/887249/


All Articles