CS 473
Homework 5 (due March 9, 2010)
Spring 2010
1. A
multistack
consists of an inﬁnite series of stacks
S
0
,
S
1
,
S
2
,..., where the
i
th stack
S
i
can hold up
to 3
i
elements. The user always pushes and pops elements from the smallest stack
S
0
. However,
before any element can be pushed onto any full stack
S
i
, we ﬁrst pop all the elements off
S
i
and
push them onto stack
S
i
+
1
to make room. (Thus, if
S
i
+
1
is already full, we ﬁrst recursively move
all its members to
S
i
+
2
.) Similarly, before any element can be popped from any empty stack
S
i
,
we ﬁrst pop 3
i
elements from
S
i
+
1
and push them onto
S
i
to make room. (Thus, if
S
i
+
1
is already
empty, we ﬁrst recursively ﬁll it by popping elements from
S
i
+
2
.) Moving a single element from
one stack to another takes
O
(
1
)
time.
Here is pseudocode for the multistack operations MSP
USH
and MSP
OP
. The internal stacks are
managed with the subroutines P
USH
and P
OP
.
MP
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Chekuri,C
 Algorithms, Analysis of algorithms, Array data structure

Click to edit the document details