103

Queue and Stack are a structures widely mentioned. However, in C++, for queue you can do it in two ways:

#include <queue>
#include <deque>

but for stack you can only do it like this

#include <stack>

My question is, what's the difference between queue and deque, why two structures proposed? For stack, any other structure could be included?

TheArchitect
  • 1,160
  • 4
  • 15
  • 26
skydoor
  • 25,218
  • 52
  • 147
  • 201

9 Answers9

90

Moron/Aryabhatta is correct, but a little more detail may be helpful.

Queue and stack are higher level containers than deque, vector, or list. By this, I mean that you can build a queue or stack out of the lower level containers.

For example:

  std::stack<int, std::deque<int> > s;
  std::queue<double, std::list<double> > q;

Will build a stack of ints using a deque as the underlying container and a queue of doubles using a list as the underlying container.

You can think of s as a restricted deque and q as a restricted list.

All that is necessary is that the lower level container implements the methods needed by the higher level container. These are back(), push_back(), and pop_back() for stack and front(), back(), push_back(), and pop_front() for queue.

See stack and queue for more detail.

With respect to the deque, it is much more than a queue where you can insert at both ends. In particular, it has the random access operator[]. This makes it more like a vector, but a vector where you can insert and delete at the beginning with push_front() and pop_front().

See deque for detail.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
81

Queue: you can insert only in one end and remove from the other.

Deque: you can insert and remove from both ends.

So using a Deque, you can model a Queue as well as a Stack.

Hint:
Deque is short for "Double ended queue".

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • 5
    will it not overkill if you use a Deque to model a stack? – skydoor Feb 11 '10 at 21:56
  • You can't model a stack with a queue. – R Samuel Klatchko Feb 11 '10 at 21:58
  • 1
    There are a lot more differences. `queue` does not satisfy the requirements of a container. It has no iterators, for heaven's sake! – Potatoswatter Feb 11 '10 at 22:06
  • @skydoor Of all the standard library containers, the deque is arguably the one with the lowest overhead. –  Feb 11 '10 at 22:13
  • "will it not overkill if you use a Deque to model a stack?" - Possibly, but as these are templates only the code you use will get put into your executable/library, – vickirk Feb 11 '10 at 22:49
  • 3
    @skydoor: Just as an FYI, the STL's `std::stack` uses a `std::deque` as the backing container by default. I speculate on the reason here: http://stackoverflow.com/questions/102459/why-does-stdstack-use-stddeque-by-default/102529#102529 (basically that growing a `deque` is low overhead). – Michael Burr Feb 12 '10 at 00:19
38

deque is a container template. It satisfies the requirements for a sequence with random-access iterators, much like a vector.

queue is not a container at all, it is an adaptor. It contains a container and provides a different, more specific interface. Use queue when you want to remember (or remind) to avoid operations besides push[_back] and pop[_front], front and back, size and empty. You can't look at elements inside the queue besides the first and last, at all!

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
25

In the C++ library, both std::stack and std::queue are implemented as container adapters. That means they provide the interface of a stack or a queue respectively, but neither is really a container in itself. Instead, they use some other container (e.g. std::deque or std::list to actually store the data), and the std::stack class just has a tiny bit of code to translate push and pop to push_back and pop_back (and std::queue does roughly the same, but using push_back and pop_front).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • For a `queue`, VS also seems to map `pop` to `pop_front`, and `push` to `push_back`, so I guess this is implementation dependent. – chappjc Aug 29 '15 at 01:40
  • @chappjc: Nope--re-checking, just my memory was off. `pop_front` and `push_back` are what's required. My apologies. – Jerry Coffin Aug 29 '15 at 07:46
8

A deque is a double-ended queue, which allows easy insertion/removal from either end. Queues only allow insertion in one end and retrieval from the other.

Michael Hackner
  • 8,605
  • 2
  • 27
  • 29
5

deque supports insert/pop from back & front

queue only supports insert to the back, and pop from the front. You know, a FIFO (first in first out).

rmn
  • 2,386
  • 1
  • 14
  • 21
0

A deque is double-ended. A queue isn't.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • Explain the implementation of stack like how it is added in queue and double ended queue(deque). – Nagappa Apr 28 '21 at 05:18
0

Priority queue dequeue happens according to some ordering (priority) comparison not the enqueue order.

For instance you might store timed events in one where you want to pull out the soonest event first and query for its scheduled time so you can sleep until that point in time.

Priority queues are often implemented using heaps.

by Mike Anderson here:
https://www.quora.com/What-is-the-difference-between-a-priority-queue-and-a-queue

Pang
  • 9,564
  • 146
  • 81
  • 122
0

In deque(double-ended queue) The element can be inserted from the back and removed from the rear(like in stack), but queue only allows removal from the front.

Nagappa
  • 194
  • 2
  • 13