
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Node Is in a Linked List in C#
Use the Contains() method to check whether a node is a LinkedList or not.
Here is our LinkedList.
string [] students = {"Beth","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students);
Now, to check whether the node “Amy” is in the list or not, we will use the Contains() method as shown below −
list.Contains("Amy")
The method will return a Boolean value i.e. True in this case.
Let us see the complete code.
Example
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Beth","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } // adding a node at the end var newNode = list.AddLast("Emma"); // adding a new node after the node added above list.AddAfter(newNode, "Matt"); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var stu in list) { Console.WriteLine(stu); } Console.WriteLine("Is Student Amy (node) in the list?: "+list.Contains("Amy")); Console.WriteLine("Is Student Anne (node) in the list?: "+list.Contains("Anne")); } }
Output
Beth Jennifer Amy Vera LinkedList after adding new nodes... Beth Jennifer Amy Vera Emma Matt Is Student Amy (node) in the list?: True Is Student Anne (node) in the list?: False
Advertisements