Node LinkedList , , .. , ?
, readonly , , readonly, . , , , !
, ?
. , , - . . , .
:
abstract class ImmutableList
{
public static readonly ImmutableList Empty = new EmptyList();
private ImmutableList() {}
public abstract int Head { get; }
public abstract ImmutableList Tail { get; }
public abstract bool IsEmpty { get; }
public abstract ImmutableList Add(int head);
private sealed class EmptyList : ImmutableList
{
public override int Head { get { throw new Exception(); } }
public override ImmutableList Tail { get { throw new Exception(); } }
public override bool IsEmpty { get { return true; } }
public override ImmutableList Add(int head)
{
return new List(head, this);
}
}
private sealed class List : ImmutableList
{
private readonly int head;
private readonly ImmutableList tail;
public override int Head { get { return head; } }
public override ImmutableList Tail { get { return tail; } }
public override bool IsEmpty { get { return false; } }
public override ImmutableList Add(int head)
{
return new List(head, this);
}
}
}
...
ImmutableList list1 = ImmutableList.Empty;
ImmutableList list2 = list1.Add(100);
ImmutableList list3 = list2.Add(400);
. , , IEnumerable<int>. . , , ; list3 2, , list2 .
- ?
, , , , ; .
, . , , - . , " ?" : " , ?" , , , .
, , , , , - deque, . :
http://blogs.msdn.com/b/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double-ended-queue.aspx
, deque, ; , , .
UPDATE:
. , w - , , ... , ?
. , (10, 20, 30, 40), 25 2? (10, 20, 25, 30, 40).
? (20, 30, 40), (30, 40) (40). , (30, 40).
. :
10 ----> 20 ----> 30 -----> 40 -----> Empty
10 ----> 20 ----> 25 -----> 30 -----> 40 -----> Empty
| 10
| /
| 10
(30, 40), .
UPDATE:
?
:
ImmutableList InsertAt(int value, int position)
{
if (position < 0)
throw new Exception();
else if (position == 0)
return this.Add(value);
else
return tail.InsertAt(value, position - 1).Add(head);
}
, ?
, , DeleteAt.
, , InsertAt DeleteAt. , , !