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

Image Noise

How To Make Rayleigh Noise On Image – C# Guide

Rayleigh noise is one of the noise models with which we can simulate data corruption. Guide to making noise from PDF and image histogram.

Posted on by Andraz Krzisnik
Using Color In Image Segmentation

How To Make Image Segmentation Work With C#

This tutorial shows how to use colors for image segmentation applied in C#. I explain the basics of applying it in different color spaces.

Posted on by Andraz Krzisnik