Posts in this Series:
Although we use data structures daily in our programs, sometimes we forget the theory behind them. So, I decided to create this series of posts to give a refresher on the data structures theory.
This is is by no means a try to replace the current data structures (for sure, the .NET ones will be better optimized than these ones), but just a way to remember what’s going on when we use them.
They will be entirely developed in C# and I will create a WPF program to show examples of their use. So, let’s start with the first one: Linked Lists.
Linked lists are lists of elements that are linked by a pointer to the next element. Each element has a structure like this:
Each node stores its data and a pointer to the next node. The last node in the list points to null. The linked list would be something like this:
(Source – Wikipedia)
The only thing that the list must store is its top node. Then, all traversing is done using the Next pointers of each node. So, a basic linked list would be like this:
This list wouldn’t do much, we need some operations. To insert some data in the list, we could use the Insert method:
I added the fields _last, that stores the last item in the list, so there is no need to traverse the list for every inserted node and _count, that stores the number of items in the list. To clear the list, we only have to set the top node to null:
To get the number of Items on the list, we could call the Count property:
We can also insert some data at a fixed position:
To insert a new node in any position, we first check for the special case to insert in the first position. If it is, we replace the top item with the inserted node and set the Next property to point to the old top. If it’s another position, we traverse the tree using the GetNodeAt, to get the item before the item to be inserted and replace the next pointer of the inserted node, pointing to where the found item was pointing and replacing the next pointer of the found item, pointing to the inserted node. The GetNodeAt function is:
With GetNodeAt, we can create an indexed property to get the data in the nth node:
To remove a node, we just need to set the next pointer of the node before to the node after the deleted one:
We find the item related to the object we want to remove and then set the next node to the value pointed to the node to be deleted. The Find method is:
We can also create a RemoveAt method:
We must still create one last method to retrieve all elements in the list:
The LinkedList class is finished, but it has one issue, here: the Data property is an Object and this brings some issues: boxing and unboxing of objects, there is no verifying of data integrity (you can add any type of object, and the objects don’t need to be related). So, the next step is to introduce a Generic Linked List, so we can solve these issues. The new NodeList is:
And the new LinkedList is:
As you can see, there was not much trouble to change from a LinkedList that stores objects to a generic Linked List.
Our Linked List is ready to be used, and in the next article we will see another data structure, the Stack.
The source code for this series of articles is in https://github.com/bsonnino/DataStructures
1 thought on “Data structures in C# – Part 1 – Linked lists”