I'm trying to get a LinkedList by user input and everything works well, but I don't understand why. In the while loop, I'm constantly setting the 'last' to be the most recent input of num. But I never update 'n1' outside of the loop, so how does it still work? When does n1 get updated and num goes in?
public static Node<int> BuildList()
{
Console.WriteLine("Enter num: -999 to finish");
int num = int.Parse(Console.ReadLine());
Node<int> n1 = new Node<int>(num);
if (num != -999)
{
Node<int> last = n1;
Console.WriteLine("Enter num: -999 to finish");
num = int.Parse(Console.ReadLine());
Console.WriteLine("If again");
while (num != -999)
{
last.SetNext(new Node<int>(num));
last = last.GetNext();
Console.WriteLine("Enter num: -999 to finish");
num = int.Parse(Console.ReadLine());
}
}
return n1;
}
CodePudding user response:
Node<T> is a reference type in C#. When you initialize Node<int> last = n1;, you're creating a new reference that points to the same Node object in memory as n1.
The first time SetNext() is called on last, it's being called on the same object as n1.
CodePudding user response:
Why & when does n1 get updated?
Node<int> last = n1;
Here you are assigning n1 linked list node to a last variable. n1 and last both are of type Node<int>. Node<T> is a reference type.
When you assign one reference type to another then, this is nothing but two variables are pointing to the same memory location. in your case, it is Node<int> location.
In simple words, n1 and last variables pointing to the same linked list node.
last.SetNext(new Node<int>(num)); //Here n1 gets updated.
this line adds the next node to the last node, ultimately you are adding to the linked list n1.
This is the reason, your n1 linked list is updating its value.
CodePudding user response:
I never update
n1outside of the loop,
Actually, it is updated before the loop when it is constructed. Both its value and next property are initialised (updated) during the execution of n1 = new Node<int>(num);. last is made a synonum for it, and in the first call to SetNext its next property is updated.
The while loop creates new nodes, and each time the previously created one gets its next property updated with the call to SetNext. Each call to SetNext makes the list one node longer as the "old" tail makes a link to the "new" tail node.
When does n1 get updated and num goes in?
num gets in the node during its construction. The node gets extended by a next node when that next node is created and SetNext is called to make the link with it.
Visualisation
It may help to see step by step what happens with an example input of:
10
20
30
-999
Before the loop starts the first node is created with the value 10, and we enter the if block, where Node<int> last = n1; is executed. This gets us in this situation;
n1
↓
┌────────────┐
│ value: 10 │
│ next: null │
└────────────┘
↑
last
Then the value 20 is read from input and the loop goes through its first iteration. The following code is executed:
last.SetNext(new Node<int>(num));
First new Node<int>(num) is executed which creates a new node, and this node reference is assigned to the next property of last. This is the result:
n1
↓
┌────────────┐ ┌────────────┐
│ value: 10 │ │ value: 20 │
│ next: ───────> │ next: null │
└────────────┘ └────────────┘
↑
last
Then last = last.GetNext(); is executed, which assigns a different node reference to last: it's the one that is referenced by last.next. So:
n1
↓
┌────────────┐ ┌────────────┐
│ value: 10 │ │ value: 20 │
│ next: ───────> │ next: null │
└────────────┘ └────────────┘
↑
last
The next number is read, 30, and a new loop iteration occurs. Again, a new node is created, and its reference is assigned to last.next with the SetNext call:
n1
↓
┌────────────┐ ┌────────────┐ ┌────────────┐
│ value: 10 │ │ value: 20 │ │ value: 30 │
│ next: ───────> │ next: ───────> │ next: null │
└────────────┘ └────────────┘ └────────────┘
↑
last
last gets assigned the successor node:
n1
↓
┌────────────┐ ┌────────────┐ ┌────────────┐
│ value: 10 │ │ value: 20 │ │ value: 30 │
│ next: ───────> │ next: ───────> │ next: null │
└────────────┘ └────────────┘ └────────────┘
↑
last
And finally, -999 is read as value and this makes the loop end. You can see that n1 references the head of the list with three nodes, which gets returned to the caller.

