c# stack queue combination

前端 未结 5 1722
南方客
南方客 2020-12-29 23:25

is there in C# some already defined generic container which can be used as Stack and as Queue at the same time? I just want to be able to append elements either to the end,

5条回答
  •  独厮守ぢ
    2020-12-30 00:10

    Here's my implementation of an immutable deque:

    http://blogs.msdn.com/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double-ended-queue.aspx

    Notice that this is an immutable double-ended-queue. Normally you probably think of a queue as something you mutate:

    queue.Enqueue(10);
    

    An immutable queue always stays the same; when you add a new element, it gives you back an entirely new queue, so you use it as:

    queue = queue.Enqueue(10);
    

    if you no longer care about the old value.

提交回复
热议问题