How To Use Linked Lists In C# – Made Easy

Linked lists are a type of list that allow us to traverse its elements without using their index positions.


Andraz Krzisnik
How To Use Linked Lists In C# – Made...

Linked lists are another type of lists, which could be a better choice in some situations where other types wouldn’t work so well. In this guide however, we’re going to use a double-linked list, because it gives us more functionality.

So what are they, what’s its specialty?

They are the kind of lists that allow us to traverse its elements by using the Next and Previous methods. In other words, there is a connection, a link, between each adjacent element. This is the property that gives us the option of calling next element in the list.

However, I want to mention that with the ordinary single-linked list, we can traverse only forward. While with a double-linked list, we can go to the next or to the previous element in the list.

Linked lists in C#

Now, let’s talk about how to use them in practice with C# and check out this pretty simple example.

LinkedList<string> playlist = new LinkedList<string>();
playlist.AddLast("Mori Calliope - guh");
playlist.AddLast("Icon For Hire - Off With Her Head");
playlist.AddLast("Jackyl - The Lumberjack");

LinkedListNode<string> current = playlist.First;
Console.WriteLine(current.Value);

while (current != null)
{
    switch (Console.ReadKey(true).Key)
    {
        case ConsoleKey.N:
            if (current.Next != null)
            {
                Console.Clear();
                current = current.Next;
                Console.WriteLine(current.Value);
            }
            break;
        case ConsoleKey.P:
            if (current.Previous != null)
            {
                Console.Clear();
                current = current.Previous;
                Console.WriteLine(current.Value);
            }
            break;
    }
}

A playlist example above utilizes a double-linked list, which allows us to go forward and backward when traversing its elements. And also, to declare it in the first place, I used LinkedList class from System.Collections.Generic namespace.

It gives us a bunch of methods to traverse it, change it and add to it. In the example I used AddLast method, which will place the new element to the last place. However, we could also use AddFirst, which would place element to the first place of the list.

But there’s more. We can also add elements in the middle by using AddBefore or AddAfter methods. However, we will need to declare a LinkedListNode for those to work since we need to specify where to include the new element.

By having a declared LinkedListNode, we can also use Remove method to remove the list node from the list.

Conclusion

I hope this guide helped you gain a better understanding about linked lists in c#.

If you want to try it out yourself, you can also download the demo project I put together.

Related Articles

C# Tutorial

C# Tutorial: How To Apply Sobel Operator To An Image

What is sobel operator? Well, basically it’s 2 kernels, with which we can process an image in a way, that only edges are visible. It is commonly used for grayscale images,...

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Thinning In Image Processing Work With C#

Thinning is a morphological operation in image processing, which eats away at the foregorund pixels and leaves only lines shaping objects.

Posted on by Andraz Krzisnik