Home  >  Article  >  Backend Development  >  C# program to find nodes in linked list

C# program to find nodes in linked list

王林
王林forward
2023-08-26 17:53:10781browse

C# program to find nodes in linked list

First, 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");

Now let’s find A node and add a new node after it -

LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");

Example

You can try running the following code to find the node in the linked list.

Live Demo

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 node = myList.Find("R");
      myList.AddAfter(node, "ADDED");
      foreach (var i in myList) {
         Console.WriteLine(i);
      }
   }
}

Output

P
Q
R
ADDED
S
T
U

The above is the detailed content of C# program to find nodes in linked list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete