
| Linear data structures |
|---|
|
Array |
In computer science theory, a deque (short for double-ended queue—usually pronounced deck) is an abstract list type data structure, also called a head-tail linked list, for which elements can be added to or removed from the front (head) or back (tail).
Contents |
Deque is sometimes written dequeue, but this use is generally deprecated in technical literature or technical writing because dequeue is also a verb meaning "to remove from a queue". Nevertheless, several libraries and some writers, such as Aho, Hopcroft, and Ullman in their textbook Data Structures and Algorithms, spell it dequeue. DEQ and DQ are also used.
This differs from the queue abstract data type or First-In-First-Out List (FIFO), where elements can only be added to one end and removed from the other. This general data class has some possible sub-types:
Both the basic and most common list types in computing, the queues and stacks can be considered specializations of deques, and can be implemented using deques.
The following operations are possible on a deque:
| operation | C++ | Java | Perl | PHP | Python | Ruby |
|---|---|---|---|---|---|---|
| insert element at back | push_back |
offerLast |
push |
array_push |
append |
push |
| insert element at front | push_front |
offerFirst |
unshift |
array_unshift |
appendleft |
unshift |
| remove last element | pop_back |
pollLast |
pop |
array_pop |
pop |
pop |
| remove first element | pop_front |
pollFirst |
shift |
array_shift |
popleft |
shift |
| examine last element | back |
peekLast |
$_[-1] |
end |
<obj>[-1] |
last |
| examine first element | front |
peekFirst |
$_[0] |
reset |
<obj>[0] |
first |
There are at least two common ways to efficiently implement a deque: with a modified dynamic array or with a doubly-linked list. For information about doubly-linked lists, see the linked list article.
Deques are often implemented using a variant of a dynamic array that can grow from both ends, sometimes called array deques. These array deques have all the properties of a dynamic array, such as constant time random access, good locality of reference, and inefficient insertion/removal in the middle, with the addition of amortized constant time insertion/removal at both ends, instead of just one end. Two common implementations include:
C++'s Standard Template Library provides the templated classes std::deque and std::list, for the dynamic array and linked list implementations, respectively.
As of Java 6, Java's Collections Framework provides a new Deque interface that provides the functionality of insertion and removal at both ends. It is implemented by classes such as ArrayDeque (also new in Java 6) and LinkedList, providing the dynamic array and linked list implementations, respectively.
Python 2.4 introduced the collections module with support for deque objects.
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History