Adding new node or value at the end of LinkedList in C#



To add new node or value at the end of 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");       Console.WriteLine("Count of nodes = " + list.Count);       Console.WriteLine("Elements in LinkedList...");       foreach (string res in list) {          Console.WriteLine(res);       }       list.AddLast("G");       list.AddLast("H");       list.AddLast("I");       Console.WriteLine("Count of nodes = " + list.Count);       Console.WriteLine("Elements in LinkedList...");       foreach (string res in list) {          Console.WriteLine(res);       }    } }

Output

This will produce the following output −

Count of nodes = 6 Elements in LinkedList... A B C D E F Count of nodes = 9 Elements in LinkedList... A B C D E F G H I

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);       Console.WriteLine("Count of nodes = " + list.Count);       Console.WriteLine("Elements in LinkedList...");       foreach (int res in list) {          Console.WriteLine(res);       }       list.AddLast(400);       list.AddLast(500);       list.AddLast(600);       Console.WriteLine("Count of nodes = " + list.Count);       Console.WriteLine("Elements in LinkedList...");       foreach (int res in list) {          Console.WriteLine(res);       }    } }

Output

This will produce the following output −

Count of nodes = 3 Elements in LinkedList... 100 200 300 Count of nodes = 6 Elements in LinkedList... 100 200 300 400 500 600
Updated on: 2019-12-06T10:18:15+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements