Collections

Linked Lists

A linked-list is a collection type that on the surface is very similar to the List<T> type, but the big difference is how the data is stored internally:

using System; using System.Collections.G...

Unlike the other standard collection types, the linked list is not backed by an array. The  LinkedList<T>  type stores values in a wrapper type called a  LinkedListNode<T> . Each node in the tree provides a reference to the next and previous nodes, forming a link (hence linked-list) between each node, in a linear order. This structure makes it quite different to a  List<T> . Here is a diagram to help you understand the structure:

To declare a linked list, you use the LinkedList<T> generic type, where T is your element type, so to declare a linked list of type string:

LinkedList<string> names = new LinkedList<string>();

The element type can be anything:

LinkedList<DateTime> calendar = new LinkedList<DateTime>();
LinkedList<int> sequence = new LinkedList<int>();

You can also use the var keyword to allow the compiler to infer the type:

var names = new LinkedList<string>();

Adding items to the list

There are several ways to add items to the list, determined by where you want to add the item. First and foremost as the list has both a head and a tail, the operations of AddFirst and AddLast are available for adding items at these positions:

names.AddFirst("Anakin");
names.AddLast("Luke");

You can also insert a value at a specific part of the list by using an instance of a LinkedListNode<T> that belongs to the list. You use this node instance, as an anchor for placing inserting the value, either before it, or after it.

To obtain an instance of the node, you can use the Find or FindLast method:

var node = names.Find("Luke");

With that node, you can then call AddBefore and AddAfter:

names.AddBefore("Padme");
names.AddAfter("Rey?");

Alternatively, you can create a LinkedListNode<T> before you add it, and then use that to anchor another value:

var node = new LinkedListNode<T>("Luke");

// Add the node
names.AddLast(node);

// Add another value, using this node
names.AddAfter(node, "Rey?");

Removing items from the list

The linked list does provide the standard Remove method, but it also provides some alternatives for removing elements at specific positions:

names.Remove("Luke");   // Find the item and remove it.
names.Remove(node);     // Remove the specific node.
names.RemoveFirst();    // Removes the first item in the list.
names.RemoveLast();     // Removes the last item in the list.

With any removal operation, the list needs to update the nodes either side of the node, so they no longer point at node, rather each other:

<-- [NodeA] <--> [NodeB] <--> [NodeC] -->   // Before removal
<-- [NodeA] <--> [NodeC] -->                // After removal

When you call the Remove method, passing the value you want to remove, the list performs a search to locate the node in the list that contains value. If you call Remove passing an instance of LinkedListNode<T>, then it will validate that the node actually belongs to the list, before removing it. If the node doesn't belong to the current list, then it throw an InvalidOperationException.

Looping through the list

Like the other standard collections, you can use the for-each, or while loop to move through the list:

foreach (string name in names)
{
    Console.WriteLine(name);    
}

 

Page 25 of 31