<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
peek	B-Application
is	O
an	O
operation	O
on	O
certain	O
abstract	O
data	O
types	O
,	O
specifically	O
sequential	O
collections	B-Application
such	O
as	O
stacks	B-Application
and	O
queues	B-Application
,	O
which	O
returns	O
the	O
value	O
of	O
the	O
top	O
(	O
"	O
front	O
"	O
)	O
of	O
the	O
collection	O
without	O
removing	O
the	O
element	O
from	O
the	O
collection	O
.	O
</s>
<s>
The	O
name	O
"	O
peek	B-Application
"	O
is	O
similar	O
to	O
the	O
basic	O
"	O
push	O
"	O
and	O
"	O
pop	O
"	O
operations	O
on	O
a	O
stack	B-Application
,	O
but	O
the	O
name	O
for	O
this	O
operation	O
varies	O
depending	O
on	O
data	O
type	O
and	O
language	O
.	O
</s>
<s>
Peek	B-Application
is	O
generally	O
considered	O
an	O
inessential	O
operation	O
,	O
compared	O
with	O
the	O
more	O
basic	O
operations	O
of	O
adding	O
and	O
removing	O
data	O
,	O
and	O
as	O
such	O
is	O
not	O
included	O
in	O
the	O
basic	O
definition	O
of	O
these	O
data	O
types	O
.	O
</s>
<s>
However	O
,	O
since	O
it	O
is	O
a	O
useful	O
operation	O
and	O
generally	O
easily	O
implemented	O
,	O
it	O
is	O
frequently	O
included	O
in	O
practices	O
,	O
and	O
in	O
some	O
definitions	O
peek	B-Application
is	O
included	O
as	O
basic	O
,	O
with	O
pop	O
(	O
or	O
analog	O
)	O
defined	O
in	O
terms	O
of	O
peek	B-Application
;	O
see	O
abstract	O
definition	O
.	O
</s>
<s>
Sequential	O
types	O
for	O
which	O
peek	B-Application
is	O
often	O
implemented	O
include	O
:	O
</s>
<s>
Single-ended	O
types	O
,	O
such	O
as	O
stack	B-Application
,	O
generally	O
only	O
admit	O
a	O
single	O
peek	B-Application
,	O
at	O
the	O
end	O
that	O
is	O
modified	O
.	O
</s>
<s>
Double-ended	O
types	O
,	O
such	O
as	O
deques	B-Application
,	O
admit	O
two	O
peeks	B-Application
,	O
one	O
at	O
each	O
end	O
.	O
</s>
<s>
Names	O
for	O
peek	B-Application
vary	O
.	O
</s>
<s>
"	O
Peek	B-Application
"	O
or	O
"	O
top	O
"	O
are	O
common	O
for	O
stacks	B-Application
,	O
while	O
for	O
queues	B-Application
"	O
front	O
"	O
is	O
common	O
.	O
</s>
<s>
Operations	O
on	O
deques	B-Application
have	O
varied	O
names	O
,	O
often	O
"	O
front	O
"	O
and	O
"	O
back	O
"	O
or	O
"	O
first	O
"	O
and	O
"	O
last	O
"	O
.	O
</s>
<s>
The	O
name	O
"	O
peak	O
"	O
is	O
also	O
occasionally	O
found	O
,	O
in	O
the	O
sense	O
of	O
"	O
top	O
,	O
summit	O
"	O
,	O
though	O
this	O
also	O
occurs	O
as	O
a	O
spelling	O
error	O
for	O
the	O
verb	O
"	O
peek	B-Application
"	O
.	O
</s>
<s>
Intuitively	O
,	O
peek	B-Application
returns	O
the	O
same	O
value	O
as	O
pop	O
,	O
but	O
does	O
not	O
change	O
the	O
data	O
.	O
</s>
<s>
Behavior	O
when	O
the	O
collection	O
is	O
empty	O
varies	O
–	O
most	O
often	O
this	O
yields	O
an	O
underflow	O
error	O
,	O
identically	O
to	O
a	O
pop	O
on	O
an	O
empty	O
collection	O
,	O
but	O
some	O
implementations	O
provide	O
a	O
function	O
which	O
instead	O
simply	O
returns	O
(	O
without	O
error	O
)	O
,	O
essentially	O
implementing	O
if	O
isempty	O
then	O
return	O
,	O
else	O
peek	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
a	O
common	O
VDM	O
(	O
Vienna	B-Application
Development	I-Application
Method	I-Application
)	O
description	O
of	O
a	O
stack	B-Application
defines	O
top	O
(	O
peek	B-Application
)	O
and	O
remove	O
as	O
atomic	O
,	O
where	O
top	O
returns	O
the	O
top	O
value	O
(	O
without	O
modifying	O
the	O
stack	B-Application
)	O
,	O
and	O
remove	O
modifies	O
the	O
stack	B-Application
(	O
without	O
returning	O
a	O
value	O
)	O
.	O
</s>
<s>
Alternatively	O
,	O
given	O
pop	O
,	O
the	O
operation	O
peek	B-Application
can	O
be	O
axiomatized	O
as	O
:	O
</s>
<s>
meaning	O
"	O
returns	O
the	O
same	O
value	O
as	O
pop	O
"	O
,	O
and	O
"	O
does	O
not	O
change	O
the	O
underlying	O
data	O
"	O
(	O
value	O
of	O
data	O
after	O
peek	B-Application
same	O
as	O
before	O
peek	B-Application
)	O
.	O
</s>
<s>
Peek	B-Application
can	O
generally	O
be	O
implemented	O
very	O
easily	O
in	O
simple	O
routine	O
taking	O
O(1 )	O
time	O
and	O
no	O
added	O
space	O
,	O
by	O
a	O
simple	O
variant	O
of	O
the	O
pop	O
operation	O
.	O
</s>
<s>
Most	O
sequential	O
data	O
types	O
are	O
implemented	O
by	O
a	O
data	O
structure	O
containing	O
a	O
reference	O
to	O
the	O
end	O
,	O
and	O
thus	O
peek	B-Application
is	O
simply	O
implemented	O
by	O
dereferencing	O
this	O
.	O
</s>
<s>
For	O
some	O
data	O
types	O
,	O
such	O
as	O
stacks	B-Application
,	O
this	O
can	O
be	O
replicated	O
in	O
terms	O
of	O
more	O
basic	O
operations	O
,	O
but	O
for	O
other	O
data	O
types	O
,	O
such	O
as	O
queues	B-Application
,	O
it	O
cannot	O
.	O
</s>
<s>
Even	O
if	O
peek	B-Application
can	O
be	O
replicated	O
in	O
terms	O
of	O
other	O
operations	O
,	O
it	O
is	O
almost	O
always	O
more	O
efficient	O
to	O
implement	O
it	O
separately	O
,	O
as	O
this	O
avoids	O
modifying	O
the	O
data	O
,	O
and	O
it	O
is	O
easy	O
to	O
implement	O
,	O
as	O
this	O
simply	O
consists	O
of	O
returning	O
the	O
same	O
value	O
as	O
the	O
"	O
pop	O
"	O
(	O
or	O
analogous	O
operation	O
)	O
,	O
but	O
then	O
not	O
modifying	O
the	O
data	O
.	O
</s>
<s>
For	O
the	O
stack	B-Application
,	O
priority	B-Application
queue	I-Application
,	O
deque	B-Application
,	O
and	O
DEPQ	B-Application
types	O
,	O
peek	B-Application
can	O
be	O
implemented	O
in	O
terms	O
of	O
pop	O
and	O
push	O
(	O
if	O
done	O
at	O
same	O
end	O
)	O
.	O
</s>
<s>
For	O
stacks	B-Application
and	O
deques	B-Application
this	O
is	O
generally	O
efficient	O
,	O
as	O
these	O
operations	O
are	O
O(1 )	O
in	O
most	O
implementations	O
,	O
and	O
do	O
not	O
require	O
memory	O
allocation	O
(	O
as	O
they	O
decrease	O
the	O
size	O
of	O
the	O
data	O
)	O
–	O
the	O
two	O
ends	O
of	O
a	O
deque	B-Application
each	O
functioning	O
as	O
a	O
stack	B-Application
.	O
</s>
<s>
For	O
priority	B-Application
queues	I-Application
and	O
DEPQs	B-Application
,	O
however	O
,	O
dequeuing	O
and	O
enqueuing	O
often	O
take	O
O(log n )	O
time	O
(	O
for	O
example	O
if	O
implemented	O
as	O
a	O
binary	B-Application
heap	I-Application
)	O
,	O
while	O
O(1 )	O
performance	O
of	O
"	O
peek	B-Application
"	O
(	O
here	O
generally	O
called	O
"	O
find-min	O
"	O
or	O
"	O
find-max	O
"	O
)	O
is	O
a	O
key	O
desired	O
characteristic	O
of	O
priority	B-Application
queues	I-Application
,	O
and	O
thus	O
peek	B-Application
is	O
almost	O
invariably	O
implemented	O
separately	O
.	O
</s>
<s>
For	O
queue	B-Application
,	O
because	O
enqueuing	O
and	O
dequeuing	O
occur	O
at	O
opposite	O
ends	O
,	O
peek	B-Application
cannot	O
be	O
implemented	O
in	O
terms	O
of	O
basic	O
operations	O
,	O
and	O
thus	O
is	O
often	O
implemented	O
separately	O
.	O
</s>
<s>
One	O
case	O
in	O
which	O
peek	B-Application
is	O
not	O
trivial	O
is	O
in	O
an	O
ordered	O
list	O
type	O
(	O
i.e.	O
,	O
elements	O
accessible	O
in	O
order	O
)	O
implemented	O
by	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
.	O
</s>
