The Ultimate Guide to Generics LinkedList<T> in C# (With Examples!)
Introduction
β Are you struggling with inserting and deleting elements efficiently in lists? π
β Do you feel that List<T>
is sometimes slow for frequent insertions? π€
Β
Well, hereβs Generics LinkedList<T> in C#, a perfect alternative to List<T>
when you need fast insertions and deletions! π
Unlike List<T>
, which stores elements in continuous memory, a LinkedList<T>
stores elements individually and links them together. This makes it super flexible for inserting and removing items.
Imagine a train π€οΈ where each compartment is linked to the next. Thatβs exactly how Generics LinkedList<T> in C# works! Letβs explore it together. π―
π What You Are Going to Learn in This Lesson?
βοΈ What is Generics LinkedList<T> in C#?
βοΈ Why is it better than List<T>
in some cases?
βοΈ Basic syntax and how it works.
βοΈ Important methods (Add, Remove, Find, etc.).
βοΈ A real-world example for better understanding.
βοΈ A complete program with detailed explanation.
Ready? Letβs go! π
π€ Why is Generics LinkedList<T> in C# Important?
- Fast insertions & deletions β No shifting like
List<T>
. - Uses memory efficiently β No need for continuous space.
- Doubly Linked β You can move forward and backward.
- Great for dynamic data structures β Like Undo/Redo, Browsing History, etc.
So, if you need fast insertions and deletions, LinkedList<T>
is the way to go! π₯
π οΈ Syntax of Generics LinkedList<T> in C#
First, letβs understand how to declare a LinkedList<T>.
LinkedList<T> listName = new LinkedList<T>();
π Explanation:
T
β Generic type (likeint
,string
,Person
, etc.).listName
β Name of the linked list.new LinkedList<T>()
β Creates an empty linked list.
Example: Creating a LinkedList of Strings
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
LinkedList<string> names = new LinkedList<string>(); // Create a LinkedList
names.AddLast("Alice"); // Add at the end
names.AddLast("Bob");
names.AddFirst("Charlie"); // Add at the beginning
Console.WriteLine("LinkedList contains: ");
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
π₯οΈ Output:
LinkedList contains:
Charlie
Alice
Bob
π― Notice how Charlie was added first, but appeared at the top because LinkedList<T> maintains order dynamically.
π Important Methods of Generics LinkedList<T> in C#
Here are some must-know methods youβll use daily! π
Method | Description | Example |
---|---|---|
AddFirst(T item) | Adds an item at the beginning. | list.AddFirst(10); |
AddLast(T item) | Adds an item at the end. | list.AddLast(20); |
Remove(T item) | Removes the first occurrence of the item. | list.Remove(10); |
RemoveFirst() | Removes the first element. | list.RemoveFirst(); |
RemoveLast() | Removes the last element. | list.RemoveLast(); |
Find(T item) | Finds an element in the list. | list.Find(20); |
Count (Property) | Returns the number of elements. | int size = list.Count; |
π‘ Example 1: Working with LinkedList<T> Methods
Letβs see these methods in action! π―
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddFirst(5);
numbers.AddLast(30);
Console.WriteLine("LinkedList: " + string.Join(", ", numbers));
numbers.Remove(20);
numbers.RemoveFirst();
Console.WriteLine("After Removing: " + string.Join(", ", numbers));
}
}
π₯οΈ Output:
LinkedList: 5, 10, 20, 30
After Removing: 10, 30
π― See how easy it is to insert and remove elements without shifting?
π Real-World Example: Browser History (Back & Forward) π
Ever used the Back and Forward buttons in a web browser?
They work just like a Generics LinkedList<T> in C#!
Β
π» Code Example: Browser History Simulation
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
LinkedList<string> browserHistory = new LinkedList<string>();
browserHistory.AddLast("Google");
browserHistory.AddLast("Facebook");
browserHistory.AddLast("YouTube");
Console.WriteLine("π Last visited: " + browserHistory.Last.Value);
browserHistory.RemoveLast(); // Going back
Console.WriteLine("π Now on: " + browserHistory.Last.Value);
}
}
π₯οΈ Output:
π Last visited: YouTube
π Now on: Facebook
π― Now, you understand how browsers handle history using a linked list! Cool, right? π
π― Conclusion
So, what did we learn today? π€
β‘οΈ LinkedList<T>
is efficient for insertions and deletions.
β‘οΈ It provides fast access to first and last elements.
β‘οΈ Used in real-world applications like browsers, undo-redo operations, and task scheduling.
Isn’t Generics LinkedList<T> in C# amazing? Now, go ahead and experiment with it! πͺ
Β
βοΈ Next What?
Ready to take your learning to the next level? π
In the next chapter, youβll learn about Generics Dictionary<TKey, TValue> in C#, which helps you store key-value pairs like a mini database! Stay tuned! π
Β
π₯ Keep coding and keep growing! π₯