LinkedList<T> Class
public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
Represents a doubly linked list of elements that can be accessed either by index or by iterating through the list. A doubly linked list is a data structure that consists of a sequence of nodes, where each node contains data and a reference (or link) to the previous and next node in the sequence.
Remarks
The LinkedList<T> class provides an efficient implementation of a doubly linked list. It allows for constant-time insertions and deletions anywhere in the list.
Each node in the linked list can be accessed via the First and Last properties, and traversal can be done using the Next and Previous properties of the LinkedListNode<T> class.
Fields
This class has no fields.
Constructors
LinkedList()
Initializes a new instance of the LinkedList<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type parameter.
LinkedList(IEqualityComparer<T> comparer)
Initializes a new instance of the LinkedList<T> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<T>.
- comparer: The
IEqualityComparer<T>implementation to use for comparing elements, ornullto use the default equality comparer for the type parameter.
Properties
| Name | Type | Description |
|---|---|---|
Count |
int | Gets the number of elements contained in the LinkedList<T>. |
First |
LinkedListNode<T> | Gets the first node of the LinkedList<T>. |
Last |
LinkedListNode<T> | Gets the last node of the LinkedList<T>. |
Methods
Adding Elements
AddAfter(LinkedListNode<T> node, T value)
Inserts a new node into the LinkedList<T> immediately after the specified node.
- node: The
LinkedListNode<T>after which to insert the new node. - value: The value to add to the end of the
LinkedList<T>.
AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode)
Inserts a new node into the LinkedList<T> immediately after the specified node.
- node: The
LinkedListNode<T>after which to insert the new node. - newNode: The
LinkedListNode<T>to insert.
AddBefore(LinkedListNode<T> node, T value)
Inserts a new node into the LinkedList<T> immediately before the specified node.
- node: The
LinkedListNode<T>before which to insert the new node. - value: The value to add to the start of the
LinkedList<T>.
AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
Inserts a new node into the LinkedList<T> immediately before the specified node.
- node: The
LinkedListNode<T>before which to insert the new node. - newNode: The
LinkedListNode<T>to insert.
AddFirst(T value)
Adds an element to the beginning of the LinkedList<T>.
- value: The value to add to the beginning of the
LinkedList<T>.
Returns: The LinkedListNode<T> that was added to the beginning of the LinkedList<T>.
AddFirst(LinkedListNode<T> newNode)
Adds a new node to the beginning of the LinkedList<T>.
- newNode: The
LinkedListNode<T>to add to the beginning of theLinkedList<T>.
AddLast(T value)
Adds an element to the end of the LinkedList<T>.
- value: The value to add to the end of the
LinkedList<T>.
Returns: The LinkedListNode<T> that was added to the end of the LinkedList<T>.
AddLast(LinkedListNode<T> newNode)
Adds a new node to the end of the LinkedList<T>.
- newNode: The
LinkedListNode<T>to add to the end of theLinkedList<T>.
Removing Elements
Remove(T value)
Removes the first occurrence of the specified value from the LinkedList<T>.
- value: The value to remove from the
LinkedList<T>.
Returns: true if the specified value is found and removed; otherwise, false.
Remove(LinkedListNode<T> node)
Removes the specified node from the LinkedList<T>.
- node: The
LinkedListNode<T>to remove from theLinkedList<T>.
RemoveFirst()
Removes and returns the first element of the LinkedList<T>.
Returns: The removed element.
RemoveLast()
Removes and returns the last element of the LinkedList<T>.
Returns: The removed element.
Clear()
Removes all elements from the LinkedList<T>.
Searching and Checking
Contains(T value)
Determines whether the LinkedList<T> contains a specific value.
- value: The value to locate in the
LinkedList<T>.
Returns: true if the specified value is found in the LinkedList<T>; otherwise, false.
Find(T value)
Searches for the specified value and returns the first LinkedListNode<T> that contains the value.
- value: The value to search for.
Returns: The first LinkedListNode<T> that contains the specified value, if found; otherwise, null.
FindLast(T value)
Searches for the specified value and returns the last LinkedListNode<T> that contains the value.
- value: The value to search for.
Returns: The last LinkedListNode<T> that contains the specified value, if found; otherwise, null.
Other Methods
CopyTo(T[] array, int index)
Copies the entire LinkedList<T> to a compatible one-dimensional Array, starting at the specified index of the target array.
- array: The one-dimensional
Arraythat is the destination of the elements copied fromLinkedList<T>. TheArraymust have zero-based indexing. - index: The zero-based index in
arrayat which copying begins.
GetEnumerator()
Returns an enumerator that iterates through the LinkedList<T>.
Returns: A LinkedList<T>.Enumerator for the LinkedList<T>.
Example Usage
using System;
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a linked list of strings
LinkedList<string> myLinkedList = new LinkedList<string>();
// Add elements to the list
myLinkedList.AddLast("Apple");
myLinkedList.AddLast("Banana");
myLinkedList.AddFirst("Orange"); // List: Orange, Apple, Banana
myLinkedList.AddAfter(myLinkedList.Find("Apple"), "Grape"); // List: Orange, Apple, Grape, Banana
// Get the first and last nodes
Console.WriteLine($"First element: {myLinkedList.First.Value}"); // Output: Orange
Console.WriteLine($"Last element: {myLinkedList.Last.Value}"); // Output: Banana
// Iterate through the list
Console.WriteLine("List elements:");
foreach (string fruit in myLinkedList)
{
Console.WriteLine(fruit);
}
// Output:
// Orange
// Apple
// Grape
// Banana
// Remove an element
myLinkedList.Remove("Grape");
Console.WriteLine($"\nAfter removing Grape:");
foreach (string fruit in myLinkedList)
{
Console.WriteLine(fruit);
}
// Output:
// Orange
// Apple
// Banana
// Check if an element exists
if (myLinkedList.Contains("Apple"))
{
Console.WriteLine("\nThe list contains Apple.");
}
// Get the node containing "Apple"
LinkedListNode<string> appleNode = myLinkedList.Find("Apple");
if (appleNode != null)
{
// Insert a new element before the "Apple" node
myLinkedList.AddBefore(appleNode, "Pear");
}
Console.WriteLine($"\nAfter adding Pear before Apple:");
foreach (string fruit in myLinkedList)
{
Console.WriteLine(fruit);
}
// Output:
// Orange
// Pear
// Apple
// Banana
}
}