Deque
A regular queue only adds at the rear and removes at the front. A double-ended queue, or deque, is more flexible: it lets you add and remove at both ends. It has 4 operations: add at the front, add at the rear, remove from the front and remove from the rear, all O(1). It's like a deck of cards where you can put and take cards on top or at the bottom.
To trace a deque by hand, you write it with the front on the left and the rear on the right. Adding at the front puts on the left; adding at the rear, on the right. Removing from the front takes the leftmost; from the rear, the rightmost. For example: rear A, rear B, front C gives [C, A, B]; removing from the rear takes B.
A deque is built with a doubly linked list or a circular array. With the doubly linked list, the head is the front and the last node is the rear; since each node knows the previous one, removing from the rear is O(1). With a singly linked list it couldn't be: to remove the last you'd have to find the second-to-last by walking everything. With a circular array, you add the operations to add at the front and remove from the rear by moving the indexes with the remainder of a division.
Since it acts as a stack and a queue, the deque is useful for everything both are, with more flexibility. One example is undo with a limit: an editor keeps changes on a stack, but only the last 50. When the 51st arrives, the oldest has to go, and it's at the bottom of the stack. A stack can't remove from the bottom, but a deque can: it adds and undoes at the rear, and drops the oldest from the front.
Stack, queue and deque are 3 ways of limiting a list so it's easier to reason about. The stack removes the most recent: it's for undoing, going back and recursion. The queue removes the oldest: it's for serving in order. The deque removes from either side: it's for when both behaviors are needed. Picking the simplest one that solves the problem makes the code clearer.
A regular queue has 2 operations: add at the rear and remove from the front. The deque adds the 2 that are missing: add at the front and remove from the rear. To know whether a problem needs a deque, just check whether at some step you have to put something in front or remove the last one that went in. If that never happens, a regular queue is enough.
