Lably
Skip to content

Double-ended queue

Abstract data type

  • 25 min read
  • 6 chapters

Related on Lably

Through Computer science, Generalization and Stack (abstract data type)

Description

The double-ended queue is most often presented as a generalized queue, which gives its name. It is a queue that "allows insertions and deletions at both ends". The deque is also described as a generalization of the stack abstract data structure, as "two stacks joined together at the base", with a shared bottom item, or as a combination of both the stack and the queue. And then conversely the queue and the stack are restricted forms of the deque.

Different analogies with real-world objects are used to describe the deque. It is compared to a "deck of cards" with which the structure shares some properties and the pronunciation. The deque is also likened to a "necklace with beads that can be added and/or removed at either end". Like the queue, it is depicted as a set of items lined up in a pipe open at both ends ; unlike the queue, the items can move in both direction in the pipe. The "railroad model" introduced by Knuth (after "Dijkstra's shunting yard") focus on the similarity with the queue, with one input and one output, and switches to select the side of the deque to be accessed.

The main operations on a deque are said to act on either side (or end) of the structure: addition of an item into the collection (enqueue), removal of an item (dequeue), and reading of an item (peek). At a given point in time only two items can be accessed, from either side of the deque, based on the history of the structure. The designated item on one side is either the latest one that has been inserted on this side (LIFO behavior), or, — if there is none —, the oldest one that has been inserted on the other side (FIFO behavior). This service policy gives the structure more flexibility and versatility, but it is also more difficult to fathom and use.

The six main operations on a deque may be described in two ways, based on the queue (or stack) operations: either by duplicating them, on each end of the structure (add_left, add_right, etc.), or by generalizing them with a parameter that indicates the location on which an operation is performed (add(left,...), add(right,...), etc.). Each approach has its merits: the first one allows the overloading of the operations but needs to have different names on each sides, while the second one simplifies the interface by halving the number of operations and allows to make use of the symmetry of the structure. In what follows, we will go with the second option to avoid needless repetition.

The deque has two other possible sub-types (more restricted forms are useless) :

  • An input-restricted deque is one where deletion can be made from either end, but insertion can be made at one end only. It is a queue with a back output, or a stack with a bottom output.
  • An output-restricted deque is one where insertion can be made at either end, but deletion can be made from one end only. It is a queue with a front input, or a stack with bottom input.

Despite their limitations these sub-types have many applications and some of their implementations may be simpler.

After Knuth, these structures are often described as different forms of restricted linear lists , or sequences. They maintain the contained elements in a given order while only allow access to some of them: the first one and/or the last one in the sequence. However other authors object that they "hide their internal dynamism from the user" and only the common implementations are linear. They consider these restricted-access data structures are either:

  • semi-structured: they have "a special or designated item but no logical relationship among the rest of the items", and "the operation that deletes an item takes no item as a parameter";
  • or point structures "because only two points of a queue exist for the outside world": the stack is then a one-point structure, while the queue and the deque are two-point structures.

For example, a deque can be implemented with a min-max heap, or with a set: before an item is inserted, it is tagged with a integer used as its value in the heap, and that depends on the history of the container. The stored elements in a heap are not ordered and their locations depend on the internal state of the structure. Another common restricted access structure is the priority queue, which is not a linear structure.

The deque can be generalized by the addition of some operations: a deque with heap order, or mindeque, allows the find-min operation. Fast or even optimal catenation is also possible.

Some authors consider the deque structure as being of little use, less useful than its restricted forms, especially the stack and the queue.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication