Clearable Stack using the
Accounting Method
• Each operation (push, pop,
clear) is charged some
number of credits
• The credits are used to pay
for computer time
– It costs one credit to do a
constant time operation
– Total time is proportional to
total number of credits
– Amortized time for an
operation is determined by
the credits charged for that
operation
Analysis
• Each operation is charged 2
credits
– O(1) amortized time per op
• For push()
– Costs one credit to do the
push()
– The other credit is “stored”
with the item on the Stack
• For clear()
– The credits in the Stack are
used to pay for clearing the
Stack
CS409 - Spring 2000
5