<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
queue	B-Application
is	O
a	O
collection	B-Application
of	O
entities	O
that	O
are	O
maintained	O
in	O
a	O
sequence	O
and	O
can	O
be	O
modified	O
by	O
the	O
addition	O
of	O
entities	O
at	O
one	O
end	O
of	O
the	O
sequence	O
and	O
the	O
removal	O
of	O
entities	O
from	O
the	O
other	O
end	O
of	O
the	O
sequence	O
.	O
</s>
<s>
By	O
convention	O
,	O
the	O
end	O
of	O
the	O
sequence	O
at	O
which	O
elements	O
are	O
added	O
is	O
called	O
the	O
back	O
,	O
tail	O
,	O
or	O
rear	O
of	O
the	O
queue	B-Application
,	O
and	O
the	O
end	O
at	O
which	O
elements	O
are	O
removed	O
is	O
called	O
the	O
head	O
or	O
front	O
of	O
the	O
queue	B-Application
,	O
analogously	O
to	O
the	O
words	O
used	O
when	O
people	O
line	O
up	O
to	O
wait	O
for	O
goods	O
or	O
services	O
.	O
</s>
<s>
The	O
operation	O
of	O
adding	O
an	O
element	O
to	O
the	O
rear	O
of	O
the	O
queue	B-Application
is	O
known	O
as	O
enqueue	O
,	O
and	O
the	O
operation	O
of	O
removing	O
an	O
element	O
from	O
the	O
front	O
is	O
known	O
as	O
dequeue	O
.	O
</s>
<s>
Other	O
operations	O
may	O
also	O
be	O
allowed	O
,	O
often	O
including	O
a	O
peek	B-Application
or	O
front	O
operation	O
that	O
returns	O
the	O
value	O
of	O
the	O
next	O
element	O
to	O
be	O
dequeued	O
without	O
dequeuing	O
it	O
.	O
</s>
<s>
The	O
operations	O
of	O
a	O
queue	B-Application
make	O
it	O
a	O
first-in-first-out	O
(	O
FIFO	B-Operating_System
)	O
data	B-General_Concept
structure	I-General_Concept
.	O
</s>
<s>
In	O
a	O
FIFO	B-Operating_System
data	B-General_Concept
structure	I-General_Concept
,	O
the	O
first	O
element	O
added	O
to	O
the	O
queue	B-Application
will	O
be	O
the	O
first	O
one	O
to	O
be	O
removed	O
.	O
</s>
<s>
A	O
queue	B-Application
is	O
an	O
example	O
of	O
a	O
linear	O
data	B-General_Concept
structure	I-General_Concept
,	O
or	O
more	O
abstractly	O
a	O
sequential	O
collection	B-Application
.	O
</s>
<s>
Queues	B-Application
are	O
common	O
in	O
computer	O
programs	O
,	O
where	O
they	O
are	O
implemented	O
as	O
data	B-General_Concept
structures	I-General_Concept
coupled	O
with	O
access	O
routines	O
,	O
as	O
an	O
abstract	O
data	B-General_Concept
structure	I-General_Concept
or	O
in	O
object-oriented	O
languages	O
as	O
classes	O
.	O
</s>
<s>
Common	O
implementations	O
are	O
circular	B-Data_Structure
buffers	I-Data_Structure
and	O
linked	B-Data_Structure
lists	I-Data_Structure
.	O
</s>
<s>
Queues	B-Application
provide	O
services	O
in	O
computer	B-General_Concept
science	I-General_Concept
,	O
transport	O
,	O
and	O
operations	O
research	O
where	O
various	O
entities	O
such	O
as	O
data	O
,	O
objects	O
,	O
persons	O
,	O
or	O
events	O
are	O
stored	O
and	O
held	O
to	O
be	O
processed	O
later	O
.	O
</s>
<s>
In	O
these	O
contexts	O
,	O
the	O
queue	B-Application
performs	O
the	O
function	O
of	O
a	O
buffer	B-General_Concept
.	O
</s>
<s>
Another	O
usage	O
of	O
queues	B-Application
is	O
in	O
the	O
implementation	O
of	O
breadth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
Theoretically	O
,	O
one	O
characteristic	O
of	O
a	O
queue	B-Application
is	O
that	O
it	O
does	O
not	O
have	O
a	O
specific	O
capacity	O
.	O
</s>
<s>
Fixed-length	O
arrays	O
are	O
limited	O
in	O
capacity	O
,	O
but	O
it	O
is	O
not	O
true	O
that	O
items	O
need	O
to	O
be	O
copied	O
towards	O
the	O
head	O
of	O
the	O
queue	B-Application
.	O
</s>
<s>
This	O
is	O
still	O
the	O
conceptually	O
simplest	O
way	O
to	O
construct	O
a	O
queue	B-Application
in	O
a	O
high-level	O
language	O
,	O
but	O
it	O
does	O
admittedly	O
slow	O
things	O
down	O
a	O
little	O
,	O
because	O
the	O
array	O
indices	O
must	O
be	O
compared	O
to	O
zero	O
and	O
the	O
array	O
size	O
,	O
which	O
is	O
comparable	O
to	O
the	O
time	O
taken	O
to	O
check	O
whether	O
an	O
array	O
index	O
is	O
out	O
of	O
bounds	O
,	O
which	O
some	O
languages	O
do	O
,	O
but	O
this	O
will	O
certainly	O
be	O
the	O
method	O
of	O
choice	O
for	O
a	O
quick	O
and	O
dirty	O
implementation	O
,	O
or	O
for	O
any	O
high-level	O
language	O
that	O
does	O
not	O
have	O
pointer	O
syntax	O
.	O
</s>
<s>
Most	O
modern	O
languages	O
with	O
objects	O
or	O
pointers	O
can	O
implement	O
or	O
come	O
with	O
libraries	O
for	O
dynamic	B-Data_Structure
lists	I-Data_Structure
.	O
</s>
<s>
Such	O
data	B-General_Concept
structures	I-General_Concept
may	O
have	O
not	O
specified	O
a	O
fixed	O
capacity	O
limit	O
besides	O
memory	O
constraints	O
.	O
</s>
<s>
Queue	B-Application
overflow	O
results	O
from	O
trying	O
to	O
add	O
an	O
element	O
onto	O
a	O
full	O
queue	B-Application
and	O
queue	B-Application
underflow	O
happens	O
when	O
trying	O
to	O
remove	O
an	O
element	O
from	O
an	O
empty	O
queue	B-Application
.	O
</s>
<s>
A	O
bounded	O
queue	B-Application
is	O
a	O
queue	B-Application
limited	O
to	O
a	O
fixed	O
number	O
of	O
items	O
.	O
</s>
<s>
There	O
are	O
several	O
efficient	O
implementations	O
of	O
FIFO	B-Operating_System
queues	B-Application
.	O
</s>
<s>
A	O
doubly	B-Data_Structure
linked	I-Data_Structure
list	I-Data_Structure
has	O
O(1 )	O
insertion	O
and	O
deletion	O
at	O
both	O
ends	O
,	O
so	O
it	O
is	O
a	O
natural	O
choice	O
for	O
queues	B-Application
.	O
</s>
<s>
A	O
regular	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
only	O
has	O
efficient	O
insertion	O
and	O
deletion	O
at	O
one	O
end	O
.	O
</s>
<s>
However	O
,	O
a	O
small	O
modification	O
—	O
keeping	O
a	O
pointer	O
to	O
the	O
last	O
node	O
in	O
addition	O
to	O
the	O
first	O
one	O
—	O
will	O
enable	O
it	O
to	O
implement	O
an	O
efficient	O
queue	B-Application
.	O
</s>
<s>
Queues	B-Application
may	O
be	O
implemented	O
as	O
a	O
separate	O
data	O
type	O
,	O
or	O
maybe	O
considered	O
a	O
special	O
case	O
of	O
a	O
double-ended	B-Application
queue	I-Application
(	O
deque	B-Application
)	O
and	O
not	O
implemented	O
separately	O
.	O
</s>
<s>
For	O
example	O
,	O
Perl	B-Language
and	O
Ruby	B-Language
allow	O
pushing	B-Application
and	I-Application
popping	I-Application
an	O
array	O
from	O
both	O
ends	O
,	O
so	O
one	O
can	O
use	O
push	O
and	O
shift	O
functions	O
to	O
enqueue	O
and	O
dequeue	O
a	O
list	O
(	O
or	O
,	O
in	O
reverse	O
,	O
one	O
can	O
use	O
unshift	O
and	O
pop	O
)	O
,	O
although	O
in	O
some	O
cases	O
these	O
operations	O
are	O
not	O
efficient	O
.	O
</s>
<s>
C++'s	O
Standard	B-Application
Template	I-Application
Library	I-Application
provides	O
a	O
"	O
queue	B-Application
"	O
templated	O
class	O
which	O
is	O
restricted	O
to	O
only	O
push/pop	B-Application
operations	O
.	O
</s>
<s>
Since	O
J2SE5.0	O
,	O
Java	O
's	O
library	O
contains	O
a	O
interface	O
that	O
specifies	O
queue	B-Application
operations	O
;	O
implementing	O
classes	O
include	O
and	O
(	O
since	O
J2SE	O
1.6	O
)	O
.	O
</s>
<s>
PHP	O
has	O
an	O
class	O
and	O
third	O
party	O
libraries	O
like	O
beanstalk	O
'd	O
and	O
Gearman	B-Application
.	O
</s>
<s>
A	O
simple	O
queue	B-Application
implemented	O
in	O
JavaScript	B-Language
:	O
</s>
<s>
Queues	B-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>
That	O
is	O
,	O
the	O
amortized	B-General_Concept
time	I-General_Concept
is	O
,	O
but	O
individual	O
operations	O
can	O
take	O
where	O
n	O
is	O
the	O
number	O
of	O
elements	O
in	O
the	O
queue	B-Application
.	O
</s>
<s>
The	O
second	O
implementation	O
is	O
called	O
a	O
real-time	O
queue	B-Application
and	O
it	O
allows	O
the	O
queue	B-Application
to	O
be	O
persistent	B-Application
with	O
operations	O
in	O
O(1 )	O
worst-case	O
time	O
.	O
</s>
<s>
This	O
queue	B-Application
's	O
data	O
is	O
stored	O
in	O
two	O
singly-linked	O
lists	O
named	O
and	O
.	O
</s>
<s>
The	O
list	O
holds	O
the	O
front	O
part	O
of	O
the	O
queue	B-Application
.	O
</s>
<s>
The	O
list	O
holds	O
the	O
remaining	O
elements	O
(	O
a.k.a.	O
,	O
the	O
rear	O
of	O
the	O
queue	B-Application
)	O
in	O
reverse	O
order	O
.	O
</s>
<s>
It	O
is	O
easy	O
to	O
insert	O
into	O
the	O
front	O
of	O
the	O
queue	B-Application
by	O
adding	O
a	O
node	O
at	O
the	O
head	O
of	O
.	O
</s>
<s>
And	O
,	O
if	O
is	O
not	O
empty	O
,	O
it	O
is	O
easy	O
to	O
remove	O
from	O
the	O
end	O
of	O
the	O
queue	B-Application
by	O
removing	O
the	O
node	O
at	O
the	O
head	O
of	O
.	O
</s>
<s>
But	O
,	O
we	O
can	O
say	O
it	O
is	O
amortized	B-General_Concept
time	I-General_Concept
,	O
because	O
every	O
element	O
in	O
had	O
to	O
be	O
inserted	O
and	O
we	O
can	O
assign	O
a	O
constant	O
cost	O
for	O
each	O
element	O
in	O
the	O
reverse	O
to	O
when	O
it	O
was	O
inserted	O
.	O
</s>
<s>
The	O
real-time	O
queue	B-Application
achieves	O
time	O
for	O
all	O
operations	O
,	O
without	O
amortization	B-General_Concept
.	O
</s>
<s>
The	O
data	B-General_Concept
structure	I-General_Concept
used	O
to	O
implement	O
our	O
queues	B-Application
consists	O
of	O
three	O
singly-linked	O
lists	O
where	O
f	O
is	O
the	O
front	O
of	O
the	O
queue	B-Application
,	O
r	O
is	O
the	O
rear	O
of	O
the	O
queue	B-Application
in	O
reverse	O
order	O
.	O
</s>
<s>
The	O
list	O
s	O
in	O
the	O
data	B-General_Concept
structure	I-General_Concept
has	O
two	O
purposes	O
.	O
</s>
