Below is a Linked List class which is storing other nodes, I want to know in function AddLast how head object is storing all other objects because when I call PrintNodes method then it print all the nodes in this order First, second and third, but I'm not getting how head is storing these objects, I want to know memory structure
public class Node
{
public Object data;
public Node next;
public Node(object data)
{
this.data = data;
}
}
public class LinkedList
{
private Node head;
private Node current;
public void Add(Node nodeToAdd)
{
nodeToAdd.next = head;
head = nodeToAdd;
}
public void AddLast(Node nodeToAdd)
{
if (head == null)
{
head = nodeToAdd;
current = head;
}
else
{
current.next = nodeToAdd;
current = current.next;
}
}
public void PrintNodes()
{
while (head != null)
{
Console.WriteLine(head.data);
head = head.next;
}
}
static void Main(string[] args)
{
LinkedList l = new LinkedList();
//l.Add(new Node("first"));
//l.Add(new Node("second"));
l.AddLast(new Node("first"));
l.AddLast(new Node("second"));
l.AddLast(new Node("third"));
l.PrintNodes();
Console.ReadKey();
}