Curriculum
In C#, a LinkedList is a collection that represents a doubly linked list of objects. Elements can be added, removed, and accessed in a flexible manner. Here’s an example of using a LinkedList to store and manipulate a collection of strings:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Create a linked list of strings
LinkedList<string> names = new LinkedList<string>();
// Add some names to the linked list
names.AddLast("Alice");
names.AddLast("Bob");
names.AddLast("Charlie");
// Insert a name at a specific position in the linked list
LinkedListNode<string> bobNode = names.Find("Bob");
names.AddBefore(bobNode, "Eve");
// Remove a name from the linked list
LinkedListNode<string> charlieNode = names.Find("Charlie");
names.Remove(charlieNode);
// Display the names in the linked list
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
In this example, we first create an empty LinkedList<string> object called names. We then use the AddLast method to add some names to the end of the linked list. We use the Find method to locate the “Bob” node, and the AddBefore method to insert a new “Eve” node before it. We use the Find method again to locate the “Charlie” node, and the Remove method to remove it from the linked list. Finally, we use a foreach loop to display the remaining names in the linked list.
Here are some other useful methods provided by the LinkedList class:
Count: Returns the number of nodes in the linked list.AddFirst/AddLast: Adds a node to the beginning/end of the linked list.AddBefore/AddAfter: Inserts a node before/after an existing node in the linked list.RemoveFirst/RemoveLast: Removes the first/last node from the linked list.Remove: Removes a specific node from the linked list.Clear: Removes all nodes from the linked list.Here’s an example that demonstrates some of these methods:
// Create a linked list of integers
LinkedList<int> numbers = new LinkedList<int>();
// Add some numbers to the linked list
numbers.AddLast(1);
numbers.AddLast(2);
numbers.AddLast(3);
// Insert a number at the beginning of the linked list
numbers.AddFirst(0);
// Remove the last number from the linked list
numbers.RemoveLast();
// Display the numbers in the linked list
foreach (int number in numbers)
{
Console.WriteLine(number);
}
// Clear the linked list
numbers.Clear();
// Display the number of nodes in the linked list (
It’s important to note that LinkedList provides many other useful properties and methods for working with linked lists, such as GetEnumerator to get an enumerator that iterates through the linked list, Contains to determine whether a value is in the linked list, and ToArray to copy the nodes of the linked list to a new array. Additionally, LinkedList provides several methods for adding and removing nodes in a more efficient manner than List, especially for larger collections, because nodes can be added or removed without needing to resize the underlying data structure.