Firstly, create a new linked list −
LinkedList<string> myList = new LinkedList<string>();
Now add some elements in the linked list −
// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");Let’s now find a node and add a new node after that −
LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");Example
You can try to run the following code to find a node in the linked list.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<string> myList = new LinkedList<string>();
// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");
LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");
foreach (var i in myList) {
Console.WriteLine(i);
}
}
}Output
P Q R ADDED S T U