29-05-2012, 05:16 PM
DATA STRUCTURES AND ALGORITHMS
DATA STRUCTURES AND ALGORITHMS.pdf (Size: 28.98 KB / Downloads: 174)
INTRODUCTION
In the last lesson on the stack interface we saw that a stack is a linear data storage structure
where items are added and removed from the same end. We described the properties of a
stack. We described what the standard stack methods, push(), pop() and peek() do. Now we
go on to describe how they do it. We shall implement the stack interface by using an array as
the underlying data storage structure.
ARRAY IMPLEMENTATION
We shall need an array of objects and two instance variables, one to hold the number of
elements in the array (capacity) and another to indicate the top of the stack.
TESTING THE ARRAY STACK
The test program shown below creates a stack with a capacity of five, pushes five objects
onto a stack, and then empties the stack item by item.