4

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.

TheAJ
  • 10,485
  • 11
  • 38
  • 57

5 Answers5

10

Check out .NET's System.Collections.Generic.LinkedList collection, which can be used as a Deque. It's a doubly linked list.

Insertion/Deletion

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42
9

PowerCollections has a Deque class (and a proven pedigree).

itowlson
  • 73,686
  • 17
  • 161
  • 157
2

List should do that for you:

var l = new List<int>();
var last = l[l.Count - 1];
l.RemoveAt(0);
Mark
  • 14,820
  • 17
  • 99
  • 159
2

Here's my implementation of a Deque<T> (using a ring buffer) and a concurrent lock-free ConcurrentDeque<T>:

Both classes support Push, Pop and Peek operations on both ends of the deque, all in O(1) time.

dcastro
  • 66,540
  • 21
  • 145
  • 155
0

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.

Community
  • 1
  • 1
mrvisser
  • 924
  • 5
  • 9
  • It should be noted that Eric's Deque implementation is an immutable collection. If immutability is not important to you, then there may be more efficient mutable deque implementations. – Stephen Drew Sep 25 '13 at 23:23