时间:2019-03-17 标签:c#stackqueuecombination
C# 中是否有一些已经定义的通用容器可以同时用作堆栈和队列? 我只是希望能够将元素附加到队列的末尾或前面,
谢谢
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, or to the front of the queue
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查 LinkedList 类。
Check the LinkedList class.
这是我对不可变双端队列的实现:
http://blogs.msdn.com/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-工作双端队列.aspx
请注意,这是一个不可变双端队列。通常,您可能会将队列视为您可以改变的东西:
不可变队列始终保持不变;当您添加新元素时,它会返回一个全新的队列,因此您可以将其用作:
如果您不再关心旧值。
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:
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:
if you no longer care about the old value.
你想要的是一个链接列表 - BCL中有一个 -具有 AddFirst 和 AddLast 方法
What you want is a linked list - there's one in the BCL - that has AddFirst and AddLast methods
这里有一个类可以帮助人们轻松实现这一点:
Here's a class to help people implement this easily:
好的旧
List
就可以做到。Add()
用于入队,Insert(0,T)
用于推送,Remove(0)
用于弹出/出队。Good old
List<T>
will do it.Add()
to enqueue,Insert(0,T)
to push,Remove(0)
to pop/dequeue.