C# program to find nodes in linked list

王林
Release: 2023-08-26 17:53:10
forward
747 people have browsed it

C# program to find nodes in linked list

First, create a new linked list -

LinkedList<string> myList = new LinkedList<string>();
Copy after login

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");
Copy after login

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

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

Example

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

Live Demo< /p>

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);
      }
   }
}
Copy after login

Output

P
Q
R
ADDED
S
T
U
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!