Is there a way to get the last element in a Queue?

前端 未结 5 1387
鱼传尺愫
鱼传尺愫 2021-01-26 18:26

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?

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 19:05

    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()

提交回复
热议问题