Reputation: 28586
I have a question about LinkedList<>
.
There are properties of this list First
and Last
.
Will that properties correct if I will set Last.AddNext(First)
?
I need to have a list where Last>Next = First
, but identify clearly the First
and Last
elements.
I have a cycle process.NextStep, NextStep
, but need to be able to identify each Step (process[i]
- the i-th Step)
.NET 2
Upvotes: 1
Views: 1737
Reputation: 1500535
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.
Upvotes: 3