I know stack is best and easiest way, yet could it be possible to obtain the last element in a queue without having to dequeue anything?
-
Why not maintain a stack as well? – Kakira Dec 08 '10 at 08:32
-
possible duplicate of [How would you obtain the first and last items in a Queue?](http://stackoverflow.com/questions/1308186/how-would-you-obtain-the-first-and-last-items-in-a-queue) – nawfal Jun 15 '14 at 19:58
6 Answers
You can simply do:
// Assumes T is a reference type, if it's a value type, then
// you will get an instance with the bits zeroed out.
T item = queue.LastOrDefault();
The problem here is that every time you want to get the last item in the Queue, you have to iterate through every item in the queue.
If it's important to you to have access to the first and last elements of a queue, then you might want to consider a double-ended queue.

- 73,706
- 19
- 184
- 253
If you really need to you can use this but consider using a different data structure:
public static class QueueExtensions<T>
{
const BindingFlags _flags =
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance;
private static readonly FieldInfo _array =
typeof(Queue<T>).GetField("_array", _flags);
private static readonly FieldInfo _size =
typeof(Queue<T>).GetField("_size", _flags);
public T LastItem(this Queue<T> value)
{
if (value == null)
throw new ArgumentNullException("value");
if (value.Count == 0)
throw new ArgumentException("The queue cannot be empty.", "value");
var array = (T[])_array.GetValue(value);
var size = (int)_size.GetValue(value);
return array[size - 1];
}
}

- 77,506
- 18
- 119
- 157
-
-1, sorry, but I just can't justify relying on an internal implementation detail like this; it's too brittle. IMO, the better option is to use a different structure (like `List
`) which allows double-ended access (and not too different performance characteristics, if that's important). – casperOne Dec 08 '10 at 13:28 -
2
-
It wouldn't work. See `Queue
` implementation by Resharper or alike - `_size` does NOT show where the last element is. Instead, there is `int _tail` field which tells where the element _after_ the last one sits. And, just `_tail-1` does NOT work either, because `_tail` can be 0 while there are elements in the queue. Queue(of T) got an effective yet tricky implementation, which does not move elements across the array on `Dequeue`, so I wouldn't recommend going Reflection path unless you're really desperate. – Mikha Sep 26 '17 at 22:53
You could use LINQ's Enumerable.Last()
method (where myQueue
is the name of your Queue):
var lastElement = myQueue.Last();
Although as others have mentioned, if you find yourself needing to do this often, you probably want to think about using a different data structure. For example, a List<T>
:
var myElement = myList[myList.Length - 1];

- 110,061
- 20
- 134
- 146
No, you would have to convert the queue to some other collection (using ToList
or ToArray
) in order to do this. This would effectively dequeue everything in the queue.
Keep in mind that by needing the last item from a queue you have effectively proven that you are using the wrong collection to hold your data. Consider changing your collection to another type that provides all the operations you need.

- 344,730
- 71
- 640
- 635
Queue's are not set up to make that operation fast, so the best you can do is O(n).
If you import System.Linq, you can write:
myQueue.Last()

- 13,585
- 1
- 17
- 12
For java 8 streams you can do:
queue.stream().reduce((first, second) -> second).get();

- 61
- 2