Stack & Queue

Stack & Queue - CS
112
:
Data
Structures
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS
112
:
Data
Structures
 Spring
2010
 Lecture
6:
Monday,
Feb
8,
2010
 Stack
and
Queue
 The
following
discussion
highlights
the
key
things
to
know/understand/remember
 about
stacks
and
queues,
especially
the
choices
in
their
implementaIon,
and
how
 these
choices
trade
off
against
each
other
 CS112


02/08/10
 Sesh
Venugopal
 2
 Stack
and
Queue
Interfaces
 The
public
constructors,
fields
and
methods
of
a
class
provide
an
interface
to
 applicaIons,
which
can
use
these
members
to
manipulate
objects
of
the
class.
The
 Stack
and
Queue
classes
have
well‐defined
interfaces
that
define
their
behavior.
 Stack
 •  push
 •  pop
 •  isEmpty
 •  size
 •  peek
 •  clear
 CS112


02/08/10
 Queue
 •  enqueue
 •  dequeue
 •  isEmpty
 •  size
 •  peek
 •  clear
 Sesh
Venugopal
 3
 Stack
and
Queue
ImplementaIon
 AlternaIve
1:
Array
 DESIGN
 Stack
 Queue
 •  Top
is
at
the
“right”
end
 •  push=>
add
to
end
of
array
 •  pop
=>
remove
from
end
of
array
 •  Front
is
lower
index,
rear
is
 higher
index
 •  Both
indexes
advance
to
right
 •  Need
to
cycle
back
to
use
empty
 space
 top=3 Recyclable,
empty
space
 front=5 CS112


02/08/10
 Sesh
Venugopal
 rear=7 4
 Stack
and
Queue
ImplementaIon
 AlternaIve
1:
Array
 Cons:
 • 

Commitment
to
array
size
upfront,
puts
burden
on
applicaIon/client
to
know/ guess
space
usage
(EXCEPTION:
bounded
queue
used
for
example
in
printers,
 which
has
a
cap
on
the
queue
size)
 • 

Code
for
“circular”
queue
somewhat
cumbersome
to
write
 ExcepIon:
 • 

Size
limit
is
needed
for
bounded
queue
(e.g.
printer
queue)
 The
primary
benefit
of
using
an
array
–
the
O(1)
Ime
to
retrieve
an
item
at
a
 given
index
–
is
not
used
at
all
and
therefore
does
not
compensate
for
the
cons.
 CS112


02/08/10
 Sesh
Venugopal
 5
 Stack
and
Queue
ImplementaIon
 AlternaIve
2:
Linked
List
 DESIGN
 Queue
 Stack
 •  Top
is
at
the
front
of
the
list
 •  push=>
add
to
front
of
list
 •  pop
=>
remove
from
front
of
list
 •  Front
is
first
node
 •  Rear
is
last
node
 •  Use
circular
linked
list
 front push
here
(add
before)
 dequeue
here
 (delete
this)
 pop
here

(delete
this)
 CS112


02/08/10
 Sesh
Venugopal
 rear enqueue
here
 (add
aher)
 6
 Stack
and
Queue
ImplementaIon
 AlternaIve
2:
Linked
List
 Pros:
 • 

Size
can
grow
as
needed
 • 

Code
is
straighjorward
 • 

push/pop
and
enqueue/dequeue
are
O(1)
Ime
(and
so
are
all
other
ops)
 CS112


02/08/10
 Sesh
Venugopal
 7
 Stack
and
Queue
ImplementaIon
 AlternaIve
3:
ArrayList
 The
java.util.ArrayList class
implements
an
auto‐expanding
array,
that
 expands
when
needed
 Constructor
ArrayList()
creates
an
array
list
with
iniIal
capacity
of
10
 IniIal
size
=
0
 Aher
execuIng
add(item)
10
Imes
(adds
to
end
of
array),
size=10
 CS112


02/08/10
 Sesh
Venugopal
 8
 Stack
and
Queue
ImplementaIon
 AlternaIve
3:
ArrayList
 When
the
next
(11th)
add(item)
is
executed,
the
array
list
auto‐expands,
as
follows:
 1.
Allocates
a
new
array,
double
the
capacity
of
the
old:
 capacity
=
20
 2.
Copies
all
the
entries
from
the
old
into
the
new
 3.
Adds
the
11th
entry
to
the
end
of
the
new
(and
the
old
is
given
up,
so
garbage
collected)
 X CS112


02/08/10
 Sesh
Venugopal
 
size
=
11
 9
 Stack
and
Queue
ImplementaIon
 AlternaIve
3:
ArrayList
 DESIGN
 Stack
 •  Same
as
array
 •  push=>
add()
to
end
of
array
 •  pop
=>
remove(lastIndex)
from
 end
of
array
 •  Worst
case
Ime
to
add()
to
 ArrayList
is
O(n),
so
worst
case
 Ime
for
push
is
O(n)
 CS112


02/08/10
 Queue
 •  Front
is
lower
index,
rear
is
 higher
index
 •  Both
indexes
advance
to
right
 •  “Runaway”
problem
–
since
array
 list
keeps
growing,
front
and
rear
 keep
advancing:
unused
space
is
 NEVER
reclaimed
 •  ArrayList
is
NOT
a
viable
soluIon
 Sesh
Venugopal
 10
 Bounded
Queue
ImplementaIon
 Size
of
array
=
bound
of
queue
=
N

(N=
8

in
this
example)
 Enqueue:
if
queue
size
!=
N,
then
rear=(rear+1)
%
N,
then
add
 Recyclable,
empty
space
 front=7 rear=7 rear=0 Mod
does
nothing
 if
rear
!=
(N‐1)
 front=7 Dequeue:
if

queue
size
!=
0,
then
return
data
at
front,
and
front=(front+1)
%
N
 front=7 rear=7 CS112


02/08/10
 front=0 Sesh
Venugopal
 rear=7 11
 Array
List
add()
Ime
–
A
Closer
Look
 Worst
case
Ime
is
O(n),
when
auto‐expanding:
all
n
items
are
copied
to
new
array
 With
an
iniIal
capacity
of
10,
and
doubling
every
Ime
when
expanding,
the
 following
graph
shows
the
number
of
array
writes
done
when
adding:
 81
 Most
add()s
are
a
single
 array
assignment
 The
spikes
are
the
worst
 case
#
array
assignments
 41
 #
of
array

 writes
 21
 11
 1
 11th
 21st
 CS112


02/08/10
 41st
 81st
 Sesh
Venugopal
 nth
add
 12
 Array
List
add()
Ime
–
Averaging
 REDISTRIBUTE
the
array
assignments
by
taking
away
each
assignment
in
a
spike
and
 give
it
to
one
of
the
adds
in
the
following
group
(unIl
the
next
spike)
instead:
rob
 the
rich
to
give
the
poor!
 81
 This
flarens
out
the
graph:
 



The
first
10
adds
=
1
assignment
 




All
other
adds
=
2
assignments
 This
gives
an
average
#
of
 assignments
of
about
2
per
add
 41
 #
of
array

 writes
 21
 Q:
What
happens
with
the
LAST
spike?
 11
 1
 11th
 21st
 CS112


02/08/10
 41st
 81st
 Sesh
Venugopal
 nth
add
 13
 Array
List
add()
Ime
–
Averaging
 Aher
redistribuIng,
the
graph
flarens
out.
The
last
spike
is
“given”
to
the
adds
that
 appear
BEFORE
it
–
80
units
can
be
given
to
the
previous
80
adds,
retaining
1.
This
 will
make
the
first
10
adds
take
on
2
units
each,
the
next
70,
3
units
each,
and
the
 remaining,
1
unit
each
 Technically,
this
is
not
the
average
work,
but
the
AMORTIZED
 work.
(Like
paying
off
a
loan
amorIzed
over
Ime.)
 #
of
array

 writes
 2
 Since
each
add
is
now
either
1,
or
2
,
or
3
 array
assignments,
the
AMORTIZED
Ime
 for
each
add
is
therefore
O(1)
 3
 11th
 CS112


02/08/10
 1
 This
last
secIon
has
at
least
one
add
 but
no
more
than
79
 81st
 Sesh
Venugopal
 nth
add
 14
 ...
View Full Document

This note was uploaded on 03/29/2012 for the course CS 112 taught by Professor Sesh during the Spring '07 term at Rutgers.

Ask a homework question - tutors are online