Queue vs Dequeue in java

user6503581 picture user6503581 · Aug 7, 2016 · Viewed 25.8k times · Source

What is the difference between them? I know that

A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue. Where as Dequeue represents a queue where you can insert and remove elements from both ends of the queue.

But which is more efficient?

Plus what's the difference between them two? cause i have a bit of knowledge about them, what i said above, but i would like to know more about them. It will be appreciated.

Answer

Dawood ibn Kareem picture Dawood ibn Kareem · Aug 7, 2016

Deque is short for "double ended queue". With an ordinary queue, you add things to one end and take them from the other. With a double ended queue, you can add things to either end, and take them from either end. That makes it a bit more versatile; for example, you could use it as a stack if you wanted to.

In terms of efficiency, it really depends on the implementation. But generally speaking, you wouldn't expect a deque to outperform a queue, because a (single ended) queue could be implemented in a way that doesn't allow objects to be added or removed at the "wrong" end. Whereas any implementation of a deque would also work as an implementation of a queue.