Introduction
The LinkedList<T>
class in C# represents a doubly linked list. Unlike arrays or lists, linked lists consist of nodes that are linked together, allowing for efficient insertions and deletions at any point in the list. The LinkedList<T>
class is part of the System.Collections.Generic
namespace and provides a flexible way to manage collections of data.
Key Features of LinkedList
- Doubly Linked: Each node points to both its previous and next nodes.
- Efficient Insertions/Deletions: Insertions and deletions are efficient, especially at the beginning and end of the list.
- Enumerability: Supports simple enumeration through the list.
- Type Safety: Enforces type safety, ensuring that all elements are of the specified type
T
.
Creating a LinkedList
Declaration and Initialization
You can declare and initialize a LinkedList<T>
in several ways:
Example
using System;
using System.Collections.Generic;
namespace LinkedListExample
{
class Program
{
static void Main(string[] args)
{
// Creating an empty linked list
LinkedList<int> numbers = new LinkedList<int>();
// Creating a linked list with initial elements
LinkedList<string> fruits = new LinkedList<string>(new[] { "Apple", "Banana", "Cherry" });
// Adding elements to the linked list
numbers.AddLast(1);
numbers.AddLast(2);
numbers.AddLast(3);
// Displaying elements
Console.WriteLine("Fruits in linked list:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
}
}
}
Output
Fruits in linked list:
Apple
Banana
Cherry
Common Operations on LinkedList
Adding Elements
- AddFirst: Adds an element at the beginning of the linked list.
numbers.AddFirst(0);
- AddLast: Adds an element at the end of the linked list.
numbers.AddLast(4);
- AddBefore: Adds an element before a specified node in the linked list.
LinkedListNode<int> node = numbers.Find(2);
numbers.AddBefore(node, 1);
- AddAfter: Adds an element after a specified node in the linked list.
numbers.AddAfter(node, 3);
Removing Elements
- Remove: Removes the first occurrence of the specified element from the linked list.
numbers.Remove(2);
- RemoveFirst: Removes the first node from the linked list.
numbers.RemoveFirst();
- RemoveLast: Removes the last node from the linked list.
numbers.RemoveLast();
- Clear: Removes all nodes from the linked list.
numbers.Clear();
Accessing Elements
- First: Gets the first node of the linked list.
LinkedListNode<int> firstNode = numbers.First;
- Last: Gets the last node of the linked list.
LinkedListNode<int> lastNode = numbers.Last;
Checking the List Size
- Count: Gets the number of nodes in the linked list.
int count = numbers.Count;
Practical Example
Let’s create a practical example where we use a LinkedList<T>
to manage a playlist of songs, allowing the user to add, remove, and display songs.
Example
using System;
using System.Collections.Generic;
namespace PlaylistExample
{
class Program
{
static void Main(string[] args)
{
LinkedList<string> playlist = new LinkedList<string>();
// Adding songs to the playlist
AddSong(playlist, "Song1.mp3");
AddSong(playlist, "Song2.mp3");
AddSong(playlist, "Song3.mp3");
// Displaying the playlist
DisplayPlaylist(playlist);
// Removing a song from the playlist
RemoveSong(playlist, "Song2.mp3");
// Displaying the updated playlist
DisplayPlaylist(playlist);
}
static void AddSong(LinkedList<string> playlist, string song)
{
playlist.AddLast(song);
Console.WriteLine($"Added: {song}");
}
static void RemoveSong(LinkedList<string> playlist, string song)
{
if (playlist.Remove(song))
{
Console.WriteLine($"Removed: {song}");
}
else
{
Console.WriteLine($"Song not found: {song}");
}
}
static void DisplayPlaylist(LinkedList<string> playlist)
{
Console.WriteLine("Playlist:");
foreach (string song in playlist)
{
Console.WriteLine(song);
}
}
}
}
Output
Added: Song1.mp3
Added: Song2.mp3
Added: Song3.mp3
Playlist:
Song1.mp3
Song2.mp3
Song3.mp3
Removed: Song2.mp3
Playlist:
Song1.mp3
Song3.mp3
Conclusion
The LinkedList<T>
class in C# provides a flexible and efficient way to manage collections of data, particularly when frequent insertions and deletions are required. Understanding how to use LinkedList<T>
effectively can help you manage collections of data in your applications, especially when the order and efficient insertion/removal of elements are important.