问题
I have been struggling trying to develop my own singly linked list, I cant understand how a node is inserted at the end of a Linked List?
Here's the code:
class LinkedList
{
private Node head;
public void AddLast(int value)
{
if (head == null)
{
head = new Node();
head.value = value;
head.next = null;
}
else
{
Node temp = new Node();
temp.value = value;
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = temp;
}
}
public void PrintAll()
{
Node current = head;
while (current != null)
{
Console.WriteLine(current.value);
current = current.next;
}
}
}
Here is the main method
static void Main(string[] args)
{
LinkedList list = new LinkedList();
list.AddLast(3);
list.AddLast(5);
list.AddLast(4);
}
1) I totally get the first part list.AddLast(3). Since head is null, we create a new node head and assign values to it.
2) When list.AddLast(5) is called, head is no more null, and thus we create a new temporary node, assign values to it. NOW we create a new node current which holds values of Head, it is to be noted Head.Next was null.
Now we iterate through current and place our temp node to current.Next.
3) Now, Upon calling list.AddLast(5) shouldn't the current be again overwritten with the contents of Head? which was Head.Value = 3 and Head.Next = Null.
So shouldn't it current.Value = 3 and Current.Next = Null? And if not then why?
回答1:
Node current = head;
When the above statement executes, current
is assigned only the reference of head
temporarily and not the value. So current
only points to head
at that moment. current.value
will give you the value for head
if executed after this statement.
while (current.next != null)
{
current = current.next;
}
Now, when the above while
loop executes, it iterates through the linked list and takes the current node to the last node in the linked list which will have current.next = null
.
current.next = temp;
When the above statement executes, the new node is added to the last of the linked list.
回答2:
You create a new instance of LinkedList
.
list.AddLast(3);
--> head
is null
, you create a new Node
and assign it to head
.
list.AddLast(5);
--> Create a new Node
temp
and assigns 5
to the value. Keep in mind that head
's value
= 3
and next
is null
. A new variable is created called current
that points to head
. The while
then traverses the tree and if current
's next
is not null, then reassign current
's next
to that node
. In this case, the while
loop is not run because head
's next
is null, so all it does is just assign head.next
to your current
node. At this point we have head.value = 3
, head.next = new Node(5, null);
list.AddLast(4);
--> Same as above but the while
loop now runs. It traverses all the way to find a Node
that has no next
value and assigns 4
to it. At this point we have head.value = 3, head.next = new Node(5, new Node(4, null))
回答3:
There are oodles of implementations of linked lists. However for a simple singlely Linked list we think of it like thus
If i understand you correctly, this is the part you don't understand
// All this part of the method does is add the new value to the end of the list
// create node
Node temp = new Node();
// assign its value
temp.value = value;
// now we want to start with the head and follow
// the chain until we find the last node
Node current = head;
// has it got a next element
while (current.next != null)
{
// if so change current to next and look again
current = current.next;
}
// yay no more nexts, lets add temp here
current.next = temp;
// you have effectively added your new element to the end of the list
On saying that another common implementation is to save the Last
/Current
Node in the class (just like you did with head).
In that case, instead of iterating through the chain we can reference that Node and just add to its next and update Last
reference in the class. However when we have to remove the last item, we have to remember not only to null
Last
in the class but null
its parents Next
reference as well.
The only way to do this is once again iterate the chain.
To make that easier we can implement a doubly linked list so its easier to find its parent
Update
what I don't understand is that why isn't the link to next nodes lost when current who holds that info is updated every time with content of Head.
In the above, think of current
as just a temporary variable, its sole job in life is just like a temporary marker of the next link in the chain,
When we call this
current = current.next;
All its doing its grabbing whats in current.next
and holding on to it for the loop condition again. We can call this without destroying whats in current.next
On the last time, when we have found nothing in current.next
i.e when the loop checks that current.next== null
we can safely put our temp node into it current.next = temp;
<== it was empty anyway
来源:https://stackoverflow.com/questions/51700696/how-a-linkedlist-keeps-track-of-all-nodes-c