C# - Java 的双端队列
in Java, there's a class called Deque, and i would like to find something similar to this in .NET (C#).
The reason i need this, is because i need to peek the last item in the collection, and then dequeue the first one in the collection.
Thanks,
AJ Ravindiran.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 .NET 的 System.Collections.Generic.LinkedList 集合,可以用作 Deque。这是一个双向链表。
插入/删除
AddFirst
:添加到开头AddLast< /code>
:添加到末尾
RemoveFirst
:从头开始删除。不返回值。RemoveLast
:从末尾删除。不返回值。浏览:
第一个
/最后
属性。这些返回一个
LinkedListNode
,而不是实际值。要获取值,您必须将.Value
添加到末尾。Check out .NET's System.Collections.Generic.LinkedList collection, which can be used as a Deque. It's a doubly linked list.
Insertion/Deletion
AddFirst
: Add to beginningAddLast
: Add to endRemoveFirst
: Remove from beginning. Does not return value.RemoveLast
: Remove from end. Does not return value.Peeking:
First
/Last
Properties.These Return a
LinkedListNode<T>
, not the actual value. To get the value you have to add.Value
to the end.PowerCollections 有一个 Deque 类(以及经过验证的血统)。
PowerCollections has a Deque class (and a proven pedigree).
列表应该为你做到这一点:
List should do that for you:
这是我的
Deque
(使用环形缓冲区)和并发无锁ConcurrentDeque
的实现:这两个类都支持双端队列两端的 Push、Pop 和 Peek 操作,所有操作都在 O(1) 时间内完成。
Here's my implementation of a
Deque<T>
(using a ring buffer) and a concurrent lock-freeConcurrentDeque<T>
:Both classes support Push, Pop and Peek operations on both ends of the deque, all in O(1) time.
在另一个SO问题中谈到了类似的事情。
流行的答案似乎是用链接列表来解决,Eric Lippert 提出了他自己的双端队列实现。
所以我想简短的答案是否定的,.NET HTH 中没有内置如此严格的数据结构
。
Something like this was touched on in another SO question .
The popular answer seemed to be settling with a linked list, and Eric Lippert suggested his own Deque implementation.
So I guess the short answer is no, there is no such strict data structure built-in in .NET
HTH.