Linked lists
While using the List
generic class, you can easily get access to particular elements of the collection using indices. However, when you get a single element, how can you move to the next element of the collection? Is it possible? To do so, you may consider the IndexOf
method to get an index of the element. Unfortunately, it returns an index of the first occurrence of a given value in the collection, so it will not always work as expected in this scenario.
It would be great to have some kind of pointer to the next element, as shown in the following diagram:
With this approach, you can easily navigate from one element to the next one using the Next
property. Such a structure is named the single-linked list. However, can it be further expanded by adding the Previous
property to allow navigating in forward and backward directions? Of course! Such a data structure is named the double-linked list and is presented in the following diagram:
As you can see, the double-linked list contains...