System.Collections.LinkedList<T> Class
Represents a doubly linked list of objects.
Namespace
System.Collections
Assembly
System.dll
Syntax
public class LinkedList<T> : ICollection, IEnumerable
Remarks
A LinkedList<T>
is a collection that contains nodes. Each node in the list contains a reference to its predecessor and successor. A LinkedList<T>
is useful when you need to frequently insert or remove elements from the beginning or end of the list, or when you need to access elements from both ends.
The LinkedList<T>
provides methods for adding, removing, and finding elements. It also provides an enumerator that can traverse the list.
Fields
There are no public fields for this class.
Properties
Name | Description |
---|---|
Count |
Gets the number of elements actually contained in the LinkedList<T> . |
First |
Gets the first node of the LinkedList<T> . |
Last |
Gets the last node of the LinkedList<T> . |
Methods
Name | Description |
---|---|
AddAfter |
Adds a new node with the specified value after the specified node. |
AddBefore |
Adds a new node with the specified value before the specified node. |
AddFirst |
Adds a new node at the beginning of the LinkedList<T> . |
AddLast |
Adds a new node at the end of the LinkedList<T> . |
Clear |
Removes all nodes from the LinkedList<T> . |
Contains |
Determines whether an element is in the LinkedList<T> . |
CopyTo |
Copies the entire LinkedList<T> to a compatible one-dimensional Array , starting at the specified index of the target array. |
Find |
Searches for the specified element and returns the first occurrence within the entire LinkedList<T> . |
FindLast |
Searches for the specified element and returns the last occurrence within the entire LinkedList<T> . |
Remove |
Removes the first occurrence of the specified value from the LinkedList<T> . |
Remove |
Removes the specified node from the LinkedList<T> . |
RemoveAll |
Removes all the elements that match the conditions defined by the specified predicate. |
RemoveFirst |
Removes the first node from the LinkedList<T> . |
RemoveLast |
Removes the last node from the LinkedList<T> . |
Example
Creating and Manipulating a LinkedList
The following example demonstrates how to create a LinkedList<T>
, add elements to it, and traverse its nodes.
using System;
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a LinkedList of strings
LinkedList<string> names = new LinkedList<string>();
// Add nodes to the end of the list
names.AddLast("Alice");
names.AddLast("Bob");
// Add nodes to the beginning of the list
names.AddFirst("Charlie");
names.AddFirst("David");
// Output the list
Console.WriteLine("Initial list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
// Expected output:
// David
// Charlie
// Alice
// Bob
// Find a node
LinkedListNode<string> aliceNode = names.Find("Alice");
// Add a node after "Alice"
if (aliceNode != null)
{
names.AddAfter(aliceNode, "Eve");
}
// Remove "Bob"
names.Remove("Bob");
// Output the modified list
Console.WriteLine("\nModified list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
// Expected output:
// David
// Charlie
// Alice
// Eve
Console.WriteLine($"\nList count: {names.Count}"); // Expected output: 4
Console.WriteLine($"First element: {names.First.Value}"); // Expected output: David
Console.WriteLine($"Last element: {names.Last.Value}"); // Expected output: Eve
}
}