Double-ended queue: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
These comments should be made on the talk page. Anyway, the concern seems academic, the names of the operations are for specific environments.
Line 1: Line 1:
In [[computer science]], a '''deque''' (short for ''double-ended [[queue (data structure)|queue]]'') is a linear data structure for which elements can be added to or removed from both ends. Thus , the terms Front or Rear may not be suitable , one can use Left and Right end instead [[User:196.205.228.198|196.205.228.198]] 00:43, 6 March 2007 (UTC). This differs from a normal queue, where elements can only be added to one end and removed from the other. Both [[queue (data structure)|queue]]s and [[stack (data structure)|stack]]s can be considered restricted [[User:196.205.228.198|196.205.228.198]] 00:43, 6 March 2007 (UTC) deques, and can be implemented using deques (where some operations are to be considered as illegal)[[User:196.205.228.198|196.205.228.198]] 00:43, 6 March 2007 (UTC).
In [[computer science]], a '''deque''' (short for ''double-ended [[queue (data structure)|queue]]'') is an [[abstract data structure]] for which elements can be added to or removed from the front or back. This differs from a normal queue, where elements can only be added to one end and removed from the other. Both [[queue (data structure)|queue]]s and [[stack (data structure)|stack]]s can be considered specializations of deques, and can be implemented using deques.


''Deque'' is usually pronounced ''deck'', possibly due to the conceptual similarity to a deck of cards, where a card can be dealt from or returned to either the face or patterned side.
''Deque'' is usually pronounced ''deck'', possibly due to the conceptual similarity to a deck of cards, where a card can be dealt from or returned to either the face or patterned side.
Line 7: Line 7:


== Operations ==
== Operations ==
I think the naming of the operations are misleading!!! A Deque is a considered a Queue in both direction. Hence the operations may be called inesrt form first, inrest from last, delete from first, delete from front BUT NEVER to say push or pop in a deque.[[User:196.205.228.198|196.205.228.198]] 00:43, 6 March 2007 (UTC)

The following operations are possible on a deque:
The following operations are possible on a deque:
{| class="wikitable"
{| class="wikitable"

Revision as of 01:54, 6 March 2007

In computer science, a deque (short for double-ended queue) is an abstract data structure for which elements can be added to or removed from the front or back. This differs from a normal queue, where elements can only be added to one end and removed from the other. Both queues and stacks can be considered specializations of deques, and can be implemented using deques.

Deque is usually pronounced deck, possibly due to the conceptual similarity to a deck of cards, where a card can be dealt from or returned to either the face or patterned side.

Naming

Deque is sometimes written dequeue, but this is generally not preferred 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.

Operations

The following operations are possible on a deque:

operation C++ Java Perl Python
insert element at back push_back offerLast push append
insert element at front push_front offerFirst unshift appendleft
remove last element pop_back pollLast pop pop
remove first element pop_front pollFirst shift popleft
examine last element back peekLast $_[$#_] <obj>[-1]
examine first element front peekFirst $_[0] <obj>[0]

Implementations

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.

Dynamic array implementation

Deque are often implemented using a dynamic array that grows in both ends, sometimes called array deques. These array deques have all the properties of a dynamic array (e.g. constant time random access, but inefficient insertion/removal in the middle, etc.), with the addition of efficient insertion/removal at both ends, instead of just one end. Implementations vary, but sometimes elements wrap around in a circular buffer, and when the buffer becomes full it is resized (like with dynamic arrays).

Language Support

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.

Complexity

See also

References

  • Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.2.1: Stacks, Queues, and Deques, pp. 238–243.