LinkedList<T>
doesn't support circular lists. From the docs:
The LinkedList<T>
class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.
There's no such method as LinkedListNode<T>.AddNext()
, but I'd expect any attempt to cause a cycle to fail with an exception.
You could always build an iterator based on a LinkedList<T>
though...
(Note that this will fail if the list is empty...)
public static IEnumerable<Tuple<T, bool, bool>> IterateInCycles<T>
(LinkedList<T> source)
{
LinkedList<T> node = source.First;
while (true)
{
yield return Tuple.Create(node.Value,
node.Previous == null,
node.Next == null);
node = node.Next ?? source.First;
}
}
Each tuple in the return sequence will be (value, isFirst, isLast)
if you see what I mean.