C# - Java's Deque
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.
PowerCollections has a Deque class (and a proven pedigree).
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.
List should do that for you:
var l = new List<int>();
var last = l[l.Count - 1];
l.RemoveAt(0);
Here's my implementation of a Deque<T>
(using a ring buffer) and a concurrent lock-free ConcurrentDeque<T>
:
- Github: https://github.com/dcastro/DequeNET
- NuGet: https://www.nuget.org/packages/DequeNET/
Both classes support Push, Pop and Peek operations on both ends of the deque, all in O(1) time.
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.
精彩评论