Find node in Linked List in C#



Lets have a look at the implementation in C#.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
  //
  // Create a new linked list.
  //
  LinkedList<string> linked = new LinkedList<string>();
  //
  // First add three elements to the linked list.
  //
  linked.AddLast("A");
  linked.AddLast("B");
  linked.AddLast("C");
  //
  // Insert a node before the second node (after the first node)
  //
  LinkedListNode<string> node = linked.Find("A");
  linked.AddAfter(node, "inserted");
  //
  // Loop through the linked list.
  //
  foreach (var value in linked)
  {
      Console.WriteLine(value);
  }
    }
}



Output:


A inserted B C



Source: csharpstar


The Tech Platform

www.thetechplatform.com

0 comments