Lesson 4
Working with C#'s LinkedList
Introduction

Welcome! In this lesson, we'll explore C#'s LinkedList<T>, a fundamental component of the .NET Collections Framework. The LinkedList<T> class provides an efficient way to implement data structures that require constant-time insertions and deletions at any point in the sequence. We'll learn how to use this class to perform operations on linked lists effectively.

Overview of C#'s LinkedList

C#'s LinkedList<T> is part of the System.Collections.Generic namespace and represents a doubly-linked list. A doubly linked list features nodes that contain references to both the previous and next nodes in the sequence, allowing for easy bidirectional traversal. This structure balances flexibility in navigation with the increased complexity and memory usage inherent in maintaining additional pointers.

Working with C#'s LinkedList

To work with LinkedList<T> in C#, we start by including the System.Collections.Generic namespace and then create an instance of LinkedList<T>:

C#
1using System; 2using System.Collections.Generic; 3 4public class Program 5{ 6 public static void Main() 7 { 8 LinkedList<string> students = new LinkedList<string>(); 9 } 10}

In this code, we've set up a LinkedList called students to store elements of type string. The list is currently empty, and we'll explore adding elements and performing other operations next.

Methods in LinkedList

C#'s LinkedList<T> provides several useful methods to manipulate the list:

  • AddLast(T value): Appends an element to the end of the list.
  • AddFirst(T value): Inserts an element at the beginning of the list.
  • RemoveFirst(): Removes the first element of the list.
  • Find(T value): Searches for the first occurrence of the specified value.

These methods are demonstrated in the following code:

C#
1using System; 2using System.Collections.Generic; 3 4public class Program 5{ 6 public static void Main() 7 { 8 LinkedList<string> students = new LinkedList<string>(); 9 10 students.AddLast("John"); 11 students.AddFirst("Alice"); 12 Console.WriteLine(students.First.Value); // prints Alice 13 students.RemoveFirst(); 14 Console.WriteLine(students.First.Value); // prints John 15 } 16}
Exploring LinkedList Traversal

Traversing a LinkedList<T> in C# can be elegantly achieved using a foreach loop to visit each node:

C#
1using System; 2using System.Collections.Generic; 3 4public class Program 5{ 6 public static void Main() 7 { 8 LinkedList<string> students = new LinkedList<string>(); 9 10 students.AddLast("John"); 11 students.AddLast("Alice"); 12 13 foreach (var student in students) 14 { 15 Console.WriteLine(student); 16 } 17 /* 18 Output: 19 John 20 Alice 21 */ 22 } 23}
Advanced LinkedList Operations

C# offers various advanced operations on LinkedList<T>:

  • AddAfter(LinkedListNode<T> node, T value): Inserts a new node after the specified existing node.
  • AddBefore(LinkedListNode<T> node, T value): Inserts a new node before the specified existing node.
  • Clear(): Removes all elements from the list.
  • Contains(T value): Checks if the list contains the specified element.
  • Count: Gets the number of elements in the list.

Here's an example of these methods in action:

C#
1using System; 2using System.Collections.Generic; 3 4public class Program 5{ 6 public static void Main() 7 { 8 LinkedList<string> students = new LinkedList<string>(); 9 10 students.AddFirst("John"); 11 students.AddLast("Alice"); 12 Console.WriteLine(students.Contains("Alice")); // prints True 13 Console.WriteLine(students.Count); // prints 2 14 students.Clear(); 15 Console.WriteLine(students.Count); // prints 0 16 } 17}
Lesson Summary and Practice

Congratulations! You've learned how to create, manipulate, and traverse LinkedList<T> in C#. We have covered numerous operations and their complexities. Now, practice implementing a LinkedList<T>, adding and removing elements dynamically, and traversing the list using the concepts you've encountered in this lesson. Exploring these exercises will solidify your understanding and prepare you for more complex data structure implementations in C#. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.