Posted on Wednesday December 22, 2010

Linked list and Double linked list in C#

Below are two examples of implementing a linked and double linked list in C#. The framework already has a [LinkedList implementation][1] from version 2.0 - it is infact a double linked list and supports a whole lot of features the example below doesn't. I wrote the code below out of curiosity more than anything (some programmers like doing this kind of thing, some don't!). Most degree students or earlier will have learnt about linked lists in their courses, some may have not.

The links at the bottom of the page give a whole host of discussions on the subject, mostly from stackoverflow. You might wonder where you’d ever need to use a (double) linked list, and you might be right when it comes .NET however the class itself is used internally by Regex and by a System.Net timer. It’s not (as I previously said here) used by stacktraces for Exceptions, these are are handled by the StackTrace and StackFrame classes.

But anything that needs a parent-child relationship makes use of a form of it, and a single linked list is an alternative form of a stack or queue.

As always with all the code on the site, if you spot a bug or something you think is a glaring load of rubbish, feel free to contact me on the contact page.

Linked List

Doubly linked list

As you can see, the implementation for a doubly linked list doesn’t differ much from a linked list. The extra functionality comes in the form of operations like deep copying, enumerators, advanced finds and inserts which I’ve left out; these are in framework’s LinkedList implementation.