The LinkedList<T> class is used to store a collection of elements as a linked list. It allows fast insertion and deletion of elements and can store duplicate values.
In this chapter, you will learn what the LinkedList<T> class is, how to create and use it, its commonly used methods, and practical examples.
The LinkedList<T> class is available in the System.Collections.Generic namespace and is used to store elements in the form of linked nodes. Each element is stored in a separate node that is connected to the next and previous nodes. It allows efficient insertion and deletion of elements at the beginning, end, or around a specific node. Unlike List<T>, it does not support direct indexing.
A generic LinkedList<T> is created by using the LinkedList<T> class. Elements can be added by using methods such as AddFirst(), AddLast(), AddBefore(), and AddAfter().
The following syntax is used to create a generic LinkedList<T> in C#:
LinkedList<T> listName = new LinkedList<T>();
In this syntax:
The following table demonstrate several methods of the LinkedList<T> Class.
| Method | Description |
|---|---|
| AddFirst() | It adds an element at the beginning of the linked list. |
| AddLast() | It adds an element at the end of the linked list. |
| AddBefore() | It inserts an element before a specified node. |
| AddAfter() | It inserts an element after a specified node. |
| Find() | It finds the first node that contains the specified value. |
| Remove() | It removes the first occurrence of the specified element. |
| RemoveFirst() | It removes the first node. |
| RemoveLast() | It removes the last node. |
| Contains() | It checks whether the linked list contains a specified value. |
| Clear() | It removes all elements from the linked list. |
| Count | It returns the total number of elements in the linked list. |
Note: Unlike List<T>, a LinkedList<T> cannot be initialized by using a collection initializer.
The following examples demonstrate how to use the LinkedList<T> class to store and manage data.
This example shows how to add elements to the beginning and end of a linked list by using the AddFirst() and AddLast() methods.
using System;
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new LinkedList<string>();
names.AddLast("Sonoo Jaiswal");
names.AddLast("Ankit");
names.AddLast("Peter");
names.AddLast("Irfan");
names.AddFirst("John");//added to first index
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Output:
John Sonoo Jaiswal Ankit Peter Irfan
Explanation
In this example, we create a LinkedList<string> and add elements by using the AddLast() method. After that, we use the AddFirst() method to insert "John" at the beginning of the linked list. Finally, we use a foreach loop to display all the elements.
This example demonstrates how to locate a node by using the Find() method and insert new elements before and after it by using the AddBefore() and AddAfter() methods.
using System;
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new LinkedList<string>();
names.AddLast("Sonoo");
names.AddLast("Ankit");
names.AddLast("Peter");
names.AddLast("Irfan");
//insert new element before "Peter"
LinkedListNode<String> node=names.Find("Peter");
names.AddBefore(node, "John");
names.AddAfter(node, "Lucy");
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Output:
Sonoo Ankit John Peter Lucy Irfan
Explanation
In this example, we first create a LinkedList<string> and add several names by using the AddLast() method. Next, we locate the node containing "Peter" by using the Find() method. After that, we insert "John" before "Peter" by using AddBefore() and "Lucy" after "Peter" by using AddAfter(). Finally, we use a foreach loop to display the updated linked list.
We request you to subscribe our newsletter for upcoming updates.