I am a beginner to C# and I am picking it up by solving data structures case scenarios. I need help visualizing what is happening in the following code snippet
public void AddAtLast(object data)
{
Node newNode = new Node();
newNode.Value = data;
current.Next = newNode;
current = newNode;
Count++;
}
What part I have understood
I am aware that a new node is being added at the end of the linked list. Also, the new node is getting its value from the function argument.
What I need help with
I am particularly thinking why current.Next
is pointing to newNode
, shouldn't it point to NULL since my newNode
will be placed at the end of the linked list and so it should point to NULL.
Also, why are we doing current=newNode
?
I understand why count++
is present probably because that want to keep track of position at which the new element is added but correct me if my understanding is wrong with this.
As we see, the last node of the linked list will have its next pointer as null since it will not have any memory address pointed to. Since each node has a pointer to the next node, data items in the linked list need not be stored at contiguous locations.
The next reference inside a node can be viewed as a link or pointer to another node. The first and last node of a linked list usually are called the head and tail of the list, respectively. Thus, we can traverse the list starting at the head and ending at the tail.
The pointer always points to the next member of the list. If the pointer is NULL, then it is the last node in the list. A linked list is held using a local pointer variable which points to the first item of the list. If that pointer is also NULL, then the list is considered to be empty.
The first node is always used as a reference to traverse the list and is called HEAD. The last node points to NULL. Declaring a Linked list : In C language, a linked list can be implemented using structure and pointers .
current
is the candidate position for next AddAtLast
operation, that is the end node of the linked list.
I understand why count++ is present probably because that want to keep track of >position at which the new element is added but correct me if my understanding >is wrong with this .
For the linked list structure you showed here, while the count
is used to keep track of the number of nodes, the current
is to keep track of current to-be-add-at-last position(that is old last node in the linked list before adding newNode) to facilitate AddAtLast
operation. After adding newNode
at old current
by AddAtLast
method, your current
will be moved and refer to updated last node(that is newNode
which was added just now).
So let's see what's happening line-by-line in the AddAtLast(object data)
method of the Linked List Class
Node newNode = new Node();
Create a new Node, this is the AddAtLast
methods goal in life
newNode.Value = data;
Assign some data to the Node
current.Next = newNode;
Assign the newNode
that was created to Current
. This is the Linked part of a Linked List
current = newNode;
Overwrite Current
(this must seem strange); I'll explain about this more later.
Count++
Increment the Count
of the Linked List
, Its nice to know the size of a list, without having to traverse all its elements. This is just a short hand way of always knowing the count.
The first thing you have to remember
Is in C# (and many other languages), objects/Classes are a Reference Type. When you create Current
(or any other object/class) you are doing 2 things.
When you overwrite a reference, you actually don't destroy the memory, just like if you scribbled out the address on a Post-It-Note and wrote something else. Your shoes still live in the cupboard. The only exception to this in .Net is if there are no more references left to you object/class the Garbage Collector (your mum) will come and clean it up and throw it away.
By calling current = newNode;
it seems like we just lost overwrote it, and lost all references to that node (we were tracking last time), but we didn't.
The second thing to remember
The Clever-Clogs who invented the Linked List knew we had to keep track of the items somehow, so they envisaged when a Node gets added, somewhere some other node needs to have a Link to it.
This is what this line of code (current.Next = newNode
) was all about. Make sure its actually linked in the list. Yeah so we overwrote it, but we now know that while someone else is Referencing the Node its not going to be cleaned up. Additionally, if we want to find it again, all we have to do is find the first Node and traverse the linkages.
Another way of thinking about it
Think of Current
as a bucket, in that bucket you have a Node, and on that Node is a piece of paper called next.
Next/Link
Post-It-Note every node has)But you have to remember, the Node you tipped out is still around somewhere (in-fact, there is likely another Node around with its name on it too, just like you wrote your new Nodes new name on it). Although, we cant access them easily, they are still there if we traverse the Linkages
In essence, this is how a Linked List works, its just a bunch of Nodes with other nodes names written on it.
We keep track of the list with tools like Current/Temp
, and First/Head
(Buckets) in the class that encapsulates this logic. Sometimes we have a Count
to make it easier to know how many nodes we are tracking. Though truly, the most important part of a Linked List is the First/Head
bucket. Without it we cannot traverse the list.
Current/Temp
in your original method just makes us easy for us to find the last node, so you don't have to traverse the list to find it
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With