<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
double-ended	B-Application
queue	I-Application
(	O
abbreviated	O
to	O
deque	B-Application
,	O
pronounced	O
deck	O
,	O
like	O
"	O
cheque	O
"	O
)	O
is	O
an	O
abstract	O
data	O
type	O
that	O
generalizes	O
a	O
queue	B-Application
,	O
for	O
which	O
elements	O
can	O
be	O
added	O
to	O
or	O
removed	O
from	O
either	O
the	O
front	O
(	O
head	O
)	O
or	O
back	O
(	O
tail	O
)	O
.	O
</s>
<s>
It	O
is	O
also	O
often	O
called	O
a	O
head-tail	B-Application
linked	I-Application
list	I-Application
,	O
though	O
properly	O
this	O
refers	O
to	O
a	O
specific	O
data	B-General_Concept
structure	I-General_Concept
implementation	O
of	O
a	O
deque	B-Application
(	O
see	O
below	O
)	O
.	O
</s>
<s>
A	O
deque	B-Application
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
that	O
allows	O
insertion	O
and	O
removal	O
of	O
elements	O
from	O
both	O
ends	O
.	O
</s>
<s>
This	O
is	O
different	O
from	O
a	O
queue	B-Application
,	O
which	O
only	O
allows	O
insertion	O
at	O
one	O
end	O
and	O
removal	O
from	O
the	O
other	O
end	O
,	O
following	O
a	O
first-in	B-Operating_System
,	I-Operating_System
first-out	I-Operating_System
(	O
FIFO	B-Operating_System
)	O
order	O
.	O
</s>
<s>
Deques	B-Application
can	O
have	O
several	O
sub-types	O
,	O
including	O
input-restricted	O
deques	B-Application
,	O
where	O
deletion	O
can	O
be	O
made	O
from	O
both	O
ends	O
but	O
insertion	O
can	O
only	O
be	O
made	O
at	O
one	O
end	O
,	O
and	O
output-restricted	O
deques	B-Application
,	O
where	O
insertion	O
can	O
be	O
made	O
at	O
both	O
ends	O
but	O
deletion	O
can	O
only	O
be	O
made	O
from	O
one	O
end	O
.	O
</s>
<s>
Deques	B-Application
are	O
a	O
fundamental	O
data	B-General_Concept
structure	I-General_Concept
in	O
computing	O
,	O
and	O
many	O
other	O
data	B-General_Concept
structures	I-General_Concept
can	O
be	O
implemented	O
using	O
them	O
.	O
</s>
<s>
For	O
example	O
,	O
queues	B-Application
and	O
stacks	B-Application
can	O
both	O
be	O
considered	O
specializations	O
of	O
deques	B-Application
.	O
</s>
<s>
The	O
basic	O
operations	O
on	O
a	O
deque	B-Application
are	O
adding	O
elements	O
to	O
either	O
end	O
and	O
removing	O
elements	O
from	O
either	O
end	O
.	O
</s>
<s>
Additionally	O
,	O
peek	B-Application
operations	O
allow	O
the	O
value	O
at	O
one	O
end	O
to	O
be	O
examined	O
without	O
removing	O
it	O
.	O
</s>
<s>
There	O
are	O
at	O
least	O
two	O
common	O
ways	O
to	O
efficiently	O
implement	O
a	O
deque	B-Application
:	O
with	O
a	O
modified	O
dynamic	B-Data_Structure
array	I-Data_Structure
or	O
with	O
a	O
doubly	B-Data_Structure
linked	I-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
Dynamic	O
array-based	O
deques	B-Application
,	O
also	O
called	O
array	O
deques	B-Application
,	O
can	O
grow	O
from	O
both	O
ends	O
and	O
have	O
all	O
the	O
properties	O
of	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
,	O
such	O
as	O
constant-time	O
random	B-General_Concept
access	I-General_Concept
and	O
good	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
,	O
with	O
the	O
addition	O
of	O
amortized	B-General_Concept
constant-time	I-General_Concept
insertion	O
and	O
removal	O
at	O
both	O
ends	O
.	O
</s>
<s>
Three	O
common	O
implementations	O
of	O
array	O
deques	B-Application
include	O
storing	O
deque	B-Application
contents	O
in	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
,	O
allocating	O
deque	B-Application
contents	O
from	O
the	O
center	O
of	O
the	O
underlying	O
array	O
,	O
and	O
storing	O
contents	O
in	O
multiple	O
smaller	O
arrays	O
.	O
</s>
<s>
The	O
other	O
common	O
implementation	O
of	O
deques	B-Application
is	O
with	O
a	O
doubly	B-Data_Structure
linked	I-Data_Structure
list	I-Data_Structure
,	O
which	O
allows	O
for	O
constant-time	O
insertion	O
and	O
removal	O
at	O
both	O
ends	O
,	O
but	O
with	O
less	O
efficient	O
random	B-General_Concept
access	I-General_Concept
than	O
dynamic	O
array-based	O
implementations	O
.	O
</s>
<s>
Deque	B-Application
is	O
sometimes	O
written	O
dequeue	O
,	O
but	O
this	O
use	O
is	O
generally	O
deprecated	O
in	O
technical	O
literature	O
or	O
technical	O
writing	O
because	O
dequeue	O
is	O
also	O
a	O
verb	O
meaning	O
"	O
to	O
remove	O
from	O
a	O
queue	B-Application
"	O
.	O
</s>
<s>
Nevertheless	O
,	O
several	O
libraries	B-Library
and	O
some	O
writers	O
,	O
such	O
as	O
Aho	O
,	O
Hopcroft	O
,	O
and	O
Ullman	O
in	O
their	O
textbook	O
Data	B-General_Concept
Structures	I-General_Concept
and	O
Algorithms	O
,	O
spell	O
it	O
dequeue	O
.	O
</s>
<s>
This	O
differs	O
from	O
the	O
queue	B-Application
abstract	O
data	O
type	O
or	O
first	B-Operating_System
in	I-Operating_System
first	I-Operating_System
out	I-Operating_System
list	O
(	O
FIFO	B-Operating_System
)	O
,	O
where	O
elements	O
can	O
only	O
be	O
added	O
to	O
one	O
end	O
and	O
removed	O
from	O
the	O
other	O
.	O
</s>
<s>
An	O
input-restricted	O
deque	B-Application
is	O
one	O
where	O
deletion	O
can	O
be	O
made	O
from	O
both	O
ends	O
,	O
but	O
insertion	O
can	O
be	O
made	O
at	O
one	O
end	O
only	O
.	O
</s>
<s>
An	O
output-restricted	O
deque	B-Application
is	O
one	O
where	O
insertion	O
can	O
be	O
made	O
at	O
both	O
ends	O
,	O
but	O
deletion	O
can	O
be	O
made	O
from	O
one	O
end	O
only	O
.	O
</s>
<s>
Both	O
the	O
basic	O
and	O
most	O
common	O
list	O
types	O
in	O
computing	O
,	O
queues	B-Application
and	O
stacks	B-Application
can	O
be	O
considered	O
specializations	O
of	O
deques	B-Application
,	O
and	O
can	O
be	O
implemented	O
using	O
deques	B-Application
.	O
</s>
<s>
The	O
basic	O
operations	O
on	O
a	O
deque	B-Application
are	O
enqueue	O
and	O
dequeue	O
on	O
either	O
end	O
.	O
</s>
<s>
Also	O
generally	O
implemented	O
are	O
peek	B-Application
operations	O
,	O
which	O
return	O
the	O
value	O
at	O
that	O
end	O
without	O
dequeuing	O
it	O
.	O
</s>
<s>
There	O
are	O
at	O
least	O
two	O
common	O
ways	O
to	O
efficiently	O
implement	O
a	O
deque	B-Application
:	O
with	O
a	O
modified	O
dynamic	B-Data_Structure
array	I-Data_Structure
or	O
with	O
a	O
doubly	B-Data_Structure
linked	I-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
The	O
dynamic	B-Data_Structure
array	I-Data_Structure
approach	O
uses	O
a	O
variant	O
of	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
that	O
can	O
grow	O
from	O
both	O
ends	O
,	O
sometimes	O
called	O
array	O
deques	B-Application
.	O
</s>
<s>
These	O
array	O
deques	B-Application
have	O
all	O
the	O
properties	O
of	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
,	O
such	O
as	O
constant-time	O
random	B-General_Concept
access	I-General_Concept
,	O
good	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
,	O
and	O
inefficient	O
insertion/removal	O
in	O
the	O
middle	O
,	O
with	O
the	O
addition	O
of	O
amortized	B-General_Concept
constant-time	I-General_Concept
insertion/removal	O
at	O
both	O
ends	O
,	O
instead	O
of	O
just	O
one	O
end	O
.	O
</s>
<s>
Storing	O
deque	B-Application
contents	O
in	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
,	O
and	O
only	O
resizing	O
when	O
the	O
buffer	O
becomes	O
full	O
.	O
</s>
<s>
Allocating	O
deque	B-Application
contents	O
from	O
the	O
center	O
of	O
the	O
underlying	O
array	O
,	O
and	O
resizing	O
the	O
underlying	O
array	O
when	O
either	O
end	O
is	O
reached	O
.	O
</s>
<s>
Indexing	O
is	O
implemented	O
by	O
keeping	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
containing	O
pointers	O
to	O
each	O
of	O
the	O
smaller	O
arrays	O
.	O
</s>
<s>
Double-ended	B-Application
queues	I-Application
can	O
also	O
be	O
implemented	O
as	O
a	O
purely	B-Application
functional	I-Application
data	I-Application
structure	I-Application
.	O
</s>
<s>
The	O
first	O
one	O
,	O
called	O
'	O
real-time	O
deque	B-Application
,	O
is	O
presented	O
below	O
.	O
</s>
<s>
It	O
allows	O
the	O
queue	B-Application
to	O
be	O
persistent	B-Application
with	O
operations	O
in	O
worst-case	O
time	O
,	O
but	O
requires	O
lazy	O
lists	O
with	O
memoization	O
.	O
</s>
<s>
Its	O
amortized	B-General_Concept
time	I-General_Concept
is	O
if	O
the	O
persistency	O
is	O
not	O
used	O
;	O
but	O
the	O
worst-time	O
complexity	O
of	O
an	O
operation	O
is	O
where	O
is	O
the	O
number	O
of	O
elements	O
in	O
the	O
double-ended	B-Application
queue	I-Application
.	O
</s>
<s>
A	O
double-ended	B-Application
queue	I-Application
is	O
represented	O
as	O
a	O
sextuple	O
(	O
len_front	O
,	O
front	O
,	O
tail_front	O
,	O
len_rear	O
,	O
rear	O
,	O
tail_rear	O
)	O
where	O
front	O
is	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
which	O
contains	O
the	O
front	O
of	O
the	O
queue	B-Application
of	O
length	O
len_front	O
.	O
</s>
<s>
Similarly	O
,	O
rear	O
is	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
which	O
represents	O
the	O
reverse	O
of	O
the	O
rear	O
of	O
the	O
queue	B-Application
,	O
of	O
length	O
len_rear	O
.	O
</s>
<s>
Note	O
that	O
,	O
when	O
a	O
double-ended	B-Application
queue	I-Application
contains	O
n	O
elements	O
in	O
the	O
front	O
list	O
and	O
n	O
elements	O
in	O
the	O
rear	O
list	O
,	O
then	O
the	O
inequality	O
invariant	O
remains	O
satisfied	O
after	O
i	O
insertions	O
and	O
d	O
deletions	O
when	O
(	O
i+d	O
)	O
≤	O
n/2	O
.	O
</s>
<s>
Let	O
us	O
first	O
give	O
an	O
implementation	O
of	O
the	O
various	O
operations	O
that	O
affect	O
the	O
front	O
of	O
the	O
deque	B-Application
-	O
cons	O
,	O
head	O
and	O
tail	O
.	O
</s>
<s>
In	O
a	O
second	O
time	O
we	O
'll	O
explain	O
how	O
to	O
modify	O
a	O
deque	B-Application
which	O
does	O
not	O
satisfy	O
the	O
invariant	O
into	O
one	O
which	O
satisfy	O
it	O
.	O
</s>
<s>
It	O
remains	O
to	O
explain	O
how	O
to	O
define	O
a	O
method	O
balance	O
that	O
rebalance	O
the	O
deque	B-Application
if	O
insert	O
 '	O
or	O
tail	O
broke	O
the	O
invariant	O
.	O
</s>
<s>
Note	O
that	O
,	O
without	O
the	O
lazy	O
part	O
of	O
the	O
implementation	O
,	O
this	O
would	O
be	O
a	O
non-persistent	O
implementation	O
of	O
queue	B-Application
in	O
amortized	B-General_Concept
time	I-General_Concept
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
lists	O
tail_front	O
and	O
tail_rear	O
could	O
be	O
removed	O
from	O
the	O
representation	O
of	O
the	O
double-ended	B-Application
queue	I-Application
.	O
</s>
<s>
Ada	B-Language
's	O
containers	O
provides	O
the	O
generic	O
packages	O
Ada.Containers.Vectors	O
and	O
Ada.Containers.Doubly_Linked_Lists	O
,	O
for	O
the	O
dynamic	B-Data_Structure
array	I-Data_Structure
and	O
linked	B-Data_Structure
list	I-Data_Structure
implementations	O
,	O
respectively	O
.	O
</s>
<s>
C++'s	O
Standard	B-Application
Template	I-Application
Library	I-Application
provides	O
the	O
class	O
templates	O
std::deque	O
and	O
std::list	O
,	O
for	O
the	O
multiple	O
array	O
and	O
linked	B-Data_Structure
list	I-Data_Structure
implementations	O
,	O
respectively	O
.	O
</s>
<s>
As	O
of	O
Java	B-Language
6	O
,	O
Java	B-Language
's	O
Collections	O
Framework	O
provides	O
a	O
new	O
interface	O
that	O
provides	O
the	O
functionality	O
of	O
insertion	O
and	O
removal	O
at	O
both	O
ends	O
.	O
</s>
<s>
It	O
is	O
implemented	O
by	O
classes	O
such	O
as	O
(	O
also	O
new	O
in	O
Java	B-Language
6	O
)	O
and	O
,	O
providing	O
the	O
dynamic	B-Data_Structure
array	I-Data_Structure
and	O
linked	B-Data_Structure
list	I-Data_Structure
implementations	O
,	O
respectively	O
.	O
</s>
<s>
However	O
,	O
the	O
ArrayDeque	O
,	O
contrary	O
to	O
its	O
name	O
,	O
does	O
not	O
support	O
random	B-General_Concept
access	I-General_Concept
.	O
</s>
<s>
Javascript	B-Language
's	O
&	O
Perl	B-Language
's	O
arrays	O
have	O
native	O
support	O
for	O
both	O
removing	O
(	O
and	O
)	O
and	O
adding	O
(	O
and	O
)	O
elements	O
on	O
both	O
ends	O
.	O
</s>
<s>
Python	B-Language
2.4	O
introduced	O
the	O
collections	O
module	O
with	O
support	O
for	O
.	O
</s>
<s>
It	O
is	O
implemented	O
using	O
a	O
doubly	B-Data_Structure
linked	I-Data_Structure
list	I-Data_Structure
of	O
fixed-length	O
subarrays	O
.	O
</s>
<s>
As	O
of	O
PHP	B-Application
5.3	O
,	O
PHP	B-Application
's	O
SPL	O
extension	O
contains	O
the	O
'	O
SplDoublyLinkedList	O
 '	O
class	O
that	O
can	O
be	O
used	O
to	O
implement	O
Deque	B-Application
datastructures	B-General_Concept
.	O
</s>
<s>
Previously	O
to	O
make	O
a	O
Deque	B-Application
structure	O
the	O
array	O
functions	O
array_shift/unshift/pop/push	O
had	O
to	O
be	O
used	O
instead	O
.	O
</s>
<s>
GHC	B-Application
's	O
module	O
implements	O
an	O
efficient	O
,	O
functional	O
deque	B-Application
structure	O
in	O
Haskell	B-Language
.	O
</s>
<s>
The	O
implementation	O
uses	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
finger	I-Data_Structure
trees	I-Data_Structure
annotated	O
with	O
sizes	O
.	O
</s>
<s>
There	O
are	O
other	O
(	O
fast	O
)	O
possibilities	O
to	O
implement	O
purely	O
functional	O
(	O
thus	O
also	O
persistent	B-Application
)	O
double	O
queues	B-Application
(	O
most	O
using	O
heavily	O
lazy	O
evaluation	O
)	O
.	O
</s>
<s>
Kaplan	O
and	O
Tarjan	O
were	O
the	O
first	O
to	O
implement	O
optimal	O
confluently	O
persistent	B-Application
catenable	O
deques	B-Application
.	O
</s>
<s>
Okasaki	O
simplified	O
the	O
data	B-General_Concept
structure	I-General_Concept
by	O
using	O
lazy	O
evaluation	O
with	O
a	O
bootstrapped	O
data	B-General_Concept
structure	I-General_Concept
and	O
degrading	O
the	O
performance	O
bounds	O
from	O
worst-case	O
to	O
amortized	B-General_Concept
.	O
</s>
<s>
Kaplan	O
,	O
Okasaki	O
,	O
and	O
Tarjan	O
produced	O
a	O
simpler	O
,	O
non-bootstrapped	O
,	O
amortized	B-General_Concept
version	O
that	O
can	O
be	O
implemented	O
either	O
using	O
lazy	O
evaluation	O
or	O
more	O
efficiently	O
using	O
mutation	O
in	O
a	O
broader	O
but	O
still	O
restricted	O
fashion	O
.	O
</s>
<s>
Mihaesau	O
and	O
Tarjan	O
created	O
a	O
simpler	O
(	O
but	O
still	O
highly	O
complex	O
)	O
strictly	O
purely	O
functional	O
implementation	O
of	O
catenable	O
deques	B-Application
,	O
and	O
also	O
a	O
much	O
simpler	O
implementation	O
of	O
strictly	O
purely	O
functional	O
non-catenable	O
deques	B-Application
,	O
both	O
of	O
which	O
have	O
optimal	O
worst-case	O
bounds	O
.	O
</s>
<s>
Rust	B-Application
's	O
std::collections	O
includes	O
which	O
implements	O
a	O
double-ended	B-Application
queue	I-Application
using	O
a	O
growable	O
ring	B-Data_Structure
buffer	I-Data_Structure
.	O
</s>
<s>
In	O
a	O
doubly-linked	B-Data_Structure
list	I-Data_Structure
implementation	O
and	O
assuming	O
no	O
allocation/deallocation	O
overhead	O
,	O
the	O
time	O
complexity	O
of	O
all	O
deque	B-Application
operations	O
is	O
O(1 )	O
.	O
</s>
<s>
Additionally	O
,	O
the	O
time	O
complexity	O
of	O
insertion	O
or	O
deletion	O
in	O
the	O
middle	O
,	O
given	O
an	O
iterator	O
,	O
is	O
O(1 )	O
;	O
however	O
,	O
the	O
time	O
complexity	O
of	O
random	B-General_Concept
access	I-General_Concept
by	O
index	O
is	O
O(n )	O
.	O
</s>
<s>
In	O
a	O
growing	O
array	O
,	O
the	O
amortized	B-General_Concept
time	I-General_Concept
complexity	O
of	O
all	O
deque	B-Application
operations	O
is	O
O(1 )	O
.	O
</s>
<s>
Additionally	O
,	O
the	O
time	O
complexity	O
of	O
random	B-General_Concept
access	I-General_Concept
by	O
index	O
is	O
O(1 )	O
;	O
but	O
the	O
time	O
complexity	O
of	O
insertion	O
or	O
deletion	O
in	O
the	O
middle	O
is	O
O(n )	O
.	O
</s>
<s>
One	O
example	O
where	O
a	O
deque	B-Application
can	O
be	O
used	O
is	O
the	O
work	B-Operating_System
stealing	I-Operating_System
algorithm	I-Operating_System
.	O
</s>
<s>
A	O
separate	O
deque	B-Application
with	O
threads	O
to	O
be	O
executed	O
is	O
maintained	O
for	O
each	O
processor	O
.	O
</s>
<s>
To	O
execute	O
the	O
next	O
thread	O
,	O
the	O
processor	O
gets	O
the	O
first	O
element	O
from	O
the	O
deque	B-Application
(	O
using	O
the	O
"	O
remove	O
first	O
element	O
"	O
deque	B-Application
operation	O
)	O
.	O
</s>
<s>
If	O
the	O
current	O
thread	O
forks	O
,	O
it	O
is	O
put	O
back	O
to	O
the	O
front	O
of	O
the	O
deque	B-Application
(	O
"	O
insert	O
element	O
at	O
front	O
"	O
)	O
and	O
a	O
new	O
thread	O
is	O
executed	O
.	O
</s>
<s>
its	O
deque	B-Application
is	O
empty	O
)	O
,	O
it	O
can	O
"	O
steal	O
"	O
a	O
thread	O
from	O
another	O
processor	O
:	O
it	O
gets	O
the	O
last	O
element	O
from	O
the	O
deque	B-Application
of	O
another	O
processor	O
(	O
"	O
remove	O
last	O
element	O
"	O
)	O
and	O
executes	O
it	O
.	O
</s>
<s>
The	O
work	B-Operating_System
stealing	I-Operating_System
algorithm	I-Operating_System
is	O
used	O
by	O
Intel	O
's	O
Threading	O
Building	O
Blocks	O
(	O
TBB	O
)	O
library	O
for	O
parallel	O
programming	O
.	O
</s>
