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.
Filter by Category
Linked lists are a type of list that allow us to traverse its elements without using their index positions.
Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.
Generic lists in C# are a data structures that allow us to add and remove objects to store inside without declaring its size.
Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.
Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.
SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.
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.
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.
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.