LinkedList<T> class in C# is the part of the removal namespace. This generic type allows fast inserting and removal of elements. It implements a classic linked list. Each object is separately allocated. In the LinkedList, certain operations do not require the whole collection to be copied. But in many common cases, LinkedList hinders performance.
- LinkedList<T> is a general-purpose linked list. It supports enumerators.
- Each node in a LinkedList<T> object is of the type LinkedListNode<T> which contains data and references to the previous and next nodes.
- The Count property is maintained internally and can be accessed in constant time without needing to traverse the list.
- The LinkedList class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.
Example: This example demonstrates how to create a LinkedList, add elements using AddLast(), and print them using the for-each loop.
C#
// C# program to add elements to a LinkedList
using System;
using System.Collections.Generic;
public class Geeks
{
public static void Main(string[] args)
{
// Creating a LinkedList
LinkedList<string> l = new LinkedList<string>();
// Adding elements to the LinkedList using AddLast() method
l.AddLast("One");
l.AddLast("Two");
l.AddLast("Three");
// Printing the LinkedList
foreach (var i in l)
{
Console.WriteLine(i);
}
}
}
Declaration of LinkedList
In C#, the LinkedList is declared as:
LinkedList<T> linkedList = new LinkedList<T>();
Constructors
Constructor | Description |
---|
LinkedList() | Initializes a new instance of the LinkedList class that is empty. |
LinkedList(IEnumerable) | Initializes a new instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied. |
LinkedList(SerializationInfo, StreamingContext) | Initializes a new instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext. |
Example: This example demonstrates how to check if the list is empty or not using Count property.
C#
// C# program to demonstrates
// how count property works
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings
LinkedList<String> l= new LinkedList<String>();
// Adding nodes in LinkedList
l.AddLast("Java");
l.AddLast("C++");
l.AddLast("C#");
l.AddLast("python");
// Checking LinkedList is empty or not
if (l.Count > 0)
Console.WriteLine("LinkedList is not empty");
else
Console.WriteLine("LinkedList is empty");
}
}
OutputLinkedList is not empty
Properties
The LinkedList<T>class provides several properties to access its state.
Properties | Description |
---|
Count | Gets the number of nodes actually contained in the LinkedList. |
First | Gets the first node of the LinkedList. |
Last | Gets the last node of the LinkedList. |
Example: This example, demonstrates how to access the Count, First and Last properities.
C#
// C# program to demonstrates how the LinkedList class
// properties like count, first and last works
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings and adding nodes
LinkedList<string> l = new LinkedList<string>();
l.AddLast("Java");
l.AddLast("C++");
l.AddLast("C#");
l.AddLast("Python");
// Displays number of nodes
Console.WriteLine("Count: " + l.Count);
// Displays first node value
Console.WriteLine("First: " + l.First.Value);
// Displays last node value
Console.WriteLine("Last: " + l.Last.Value);
}
}
OutputCount: 4
First: Java
Last: Python
Methods
Method | Description |
---|
AddAfter() | Adds a new node or value after an existing node in the LinkedList. |
AddBefore() | Adds a new node or value before an existing node in the LinkedList. |
AddFirst() | Adds a new node or value at the start of the LinkedList. |
AddLast() | Adds a new node or value at the end of the LinkedList. |
Clear() | Removes all nodes from the LinkedList. |
Contains(T) | Determines whether a value is in the LinkedList. |
CopyTo(T[], Int32) | Copies the entire LinkedList to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
Find(T) | Finds the first node that contains the specified value. |
FindLast(T) | Finds the last node that contains the specified value. |
GetEnumerator() | Returns an enumerator that iterates through the LinkedList. |
GetHashCode() | Serves as the default hash function. |
GetObjectData(SerializationInfo, StreamingContext) | Implements the ISerializable interface and returns the data needed to serialize the LinkedList instance. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
OnDeserialization(Object) | Implements the ISerializable interface and raises the deserialization event when the deserialization is complete. |
Remove(LinkedListNode) | Removes the specified node from the LinkedList. |
Remove(T) | Removes the first occurrence of the specified value from the LinkedList. |
RemoveFirst() | Removes the node at the start of the LinkedList. |
RemoveLast() | Removes the node at the end of the LinkedList. |
ToString() | Returns a string that represents the current object. |
Example 1: This example demonstrates how to check if the specified element present in the list using the Contains() method.
C#
// C# program to check if the specified
// element is present ot not
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings
LinkedList<String> l = new LinkedList<String>();
// Adding nodes in LinkedList
l.AddLast("A");
l.AddLast("B");
l.AddLast("C");
l.AddLast("D");
l.AddLast("E");
// To check if a value is in LinkedList
Console.WriteLine("Element B is presene in the List: "
+ l.Contains("B"));
}
}
OutputElement B is presene in the List: True
Example 2: This example demonstrates how to remove the first node using Remove() and display the list before and after removal.
C#
// C# Program to demonstrates how to
// remove specific node from the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Integers
LinkedList<int> l = new LinkedList<int>();
// Adding nodes in LinkedList
l.AddLast(2);
l.AddLast(4);
l.AddLast(6);
l.AddLast(8);
// To get the count of nodes in LinkedList
// before removing all the nodes
Console.WriteLine("Total nodes in LinkedList are : "
+ l.Count);
// Displaying the nodes in LinkedList
foreach(int i in l) {
Console.WriteLine(i);
}
// Removing the first node from the LinkedList
l.Remove(l.First);
// To get the count of nodes in LinkedList
// after removing all the nodes
Console.WriteLine("Total nodes in LinkedList are : "
+ l.Count);
// Displaying the nodes in LinkedList
foreach(int i in l) {
Console.WriteLine(i);
}
}
}
OutputTotal nodes in LinkedList are : 4
2
4
6
8
Total nodes in LinkedList are : 3
4
6
8