Get the first node of the LinkedList in C#


To get the first node of the LinkedList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<String> list = new LinkedList<String>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("D");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      list.AddLast("J");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = "+list.First.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

Output

This will produce the following output −

Count of nodes = 10
First Node = A
Count of nodes (updated) = 0

Example

Let us see another example −

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      list.AddLast(400);
      list.AddLast(500);
      list.AddLast(600);
      list.AddLast(700);
      list.AddLast(800);
      list.AddLast(900);
      list.AddLast(1000);
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = "+list.First.Value);
      Console.WriteLine("Last Node = "+list.Last.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

Output

This will produce the following output −

Count of nodes = 10
First Node = 100
Last Node = 1000
Count of nodes (updated) = 0
raja
Published on 04-Dec-2019 08:10:52
Advertisements