Complete C# Tutorial

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. 🎯

πŸ€” 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 (like int, 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! πŸš€

MethodDescriptionExample
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! πŸ”₯

Leave a Comment

Share this Doc

Generics LinkedList<T>

Or copy link