Deque<T>
is a .NET implementation of double-ended queue.
using Unageek.Collections;
var queue = new Deque<string>();
queue.AddLast("one");
queue.AddLast("two");
queue.AddLast("three");
var stack = new Deque<string>();
stack.AddFirst("three");
stack.AddFirst("two");
stack.AddFirst("one");
Console.WriteLine(queue.SequenceEqual(stack)); // True
-
Drop-in replacement for
List<T>
Deque<T>
implements the same interfaces, methods, and properties asList<T>
, including random access, bulk insertion/removal, and binary search. -
Performance
Random insertion and removal are expected to be twice as fast as
List<T>
. -
Additional methods
Deque<T>
also provides the following methods:void AddFirst(T item)
void AddLast(T item)
(equivalent toAdd
)void RemoveFirst()
void RemoveLast()
T PeekFirst()
T PeekLast()
T PopFirst()
T PopLast()
bool TryPeekFirst(out T item)
bool TryPeekLast(out T item)
bool TryPopFirst(out T item)
bool TryPopLast(out T item)
-
The maximum number of elements a
Deque<T>
can hold is 230 = 1,073,741,824. If you try to add more items, anOutOfMemoryException
is thrown. -
The capacity of a
Deque<T>
can only be zero or a power of two (up to 230). -
Serialization is not supported.
.NET 6+ is supported.