Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to check whether a node is a LinkedList or not
The Contains() method in C# is used to check whether a specific value exists in a LinkedList<T>. This method searches through all nodes in the LinkedList and returns a boolean value indicating whether the specified element is found.
Syntax
Following is the syntax for using the Contains() method −
public bool Contains(T item)
Parameters
- item − The value to locate in the LinkedList.
Return Value
Returns true if the item is found in the LinkedList; otherwise, false.
Example
Here's how to check whether specific values exist in a LinkedList −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] students = {"Beth", "Jennifer", "Amy", "Vera"};
LinkedList<string> list = new LinkedList<string>(students);
Console.WriteLine("Original LinkedList:");
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("\nLinkedList after adding new nodes:");
foreach (var stu in list) {
Console.WriteLine(stu);
}
Console.WriteLine("\nChecking if nodes exist:");
Console.WriteLine("Is Student Amy in the list?: " + list.Contains("Amy"));
Console.WriteLine("Is Student Anne in the list?: " + list.Contains("Anne"));
Console.WriteLine("Is Student Matt in the list?: " + list.Contains("Matt"));
}
}
The output of the above code is −
Original LinkedList: Beth Jennifer Amy Vera LinkedList after adding new nodes: Beth Jennifer Amy Vera Emma Matt Checking if nodes exist: Is Student Amy in the list?: True Is Student Anne in the list?: False Is Student Matt in the list?: True
Using Contains() with Different Data Types
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
// Integer LinkedList
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddLast(30);
Console.WriteLine("Number 20 exists: " + numbers.Contains(20));
Console.WriteLine("Number 25 exists: " + numbers.Contains(25));
// Custom object LinkedList
LinkedList<string> colors = new LinkedList<string>();
colors.AddLast("Red");
colors.AddLast("Green");
colors.AddLast("Blue");
Console.WriteLine("Color 'Green' exists: " + colors.Contains("Green"));
Console.WriteLine("Color 'Yellow' exists: " + colors.Contains("Yellow"));
}
}
The output of the above code is −
Number 20 exists: True Number 25 exists: False Color 'Green' exists: True Color 'Yellow' exists: False
How It Works
The Contains() method performs a linear search through the LinkedList, starting from the first node and comparing each element with the specified value using the default equality comparer. The time complexity is O(n) where n is the number of elements in the LinkedList.
Conclusion
The Contains() method provides a simple way to check if a specific value exists in a LinkedList. It returns a boolean value and works with any data type that the LinkedList contains, making it a versatile tool for searching operations.
