A linked list is a special list whereby each element (or per-element container object) in the list has a direct reference (the "link") to the next item in the list. This type of list is not implemented using an array.
A singly-linked list usually only has a link to the next item with the final item being null to indicate the end of the list.
A doubly-linked list has a link to both the next and previous items with nulls to indicate each end of the list.
The advantage with a linked list is that inserts and removals are exceedingly quick. Iterating over the entire list also has good performance, but non-linear searching can be slow.
Typically an implementation of a linked list should implement the IEnumerable<T>
interface. Implementing IList<T>
would promote the use of inefficient linear searches.
The .NET implementation of a linked list has the following declaration (minus some irrelevant cruft).
LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
As with the IList<T>
interface I don't see why the ICollection
& ICollection<T>
interfaces have been implemented, but they have.
The element container object (that has the links) looks like this:
public sealed class LinkedListNode<T>
{
public LinkedListNode<T> Next { get; }
public LinkedListNode<T> Previous { get; }
public T Value { get; set; }
}
How's that?