Array-Based Stacks
Figure 4.18 shows a complete implementation for the array-based stack class. As
with the array-based list implementation, listArray must be declared of fixed
size when the stack is created. In the stack constructor, size serves to indicate
this size. Method top acts somewhat like a current position value (because the
“current” position is always at the top of the stack), as well as indicating the number
of elements currently in the stack.
The array-based stack implementation is essentially a simplified version of the
array-based list. The only important design decision to be made is which end of
the array should represent the top of the stack. One choice is to make the top be
at position 0 in the array. In terms of list functions, all insert and remove
operations would then be on the element in position 0. This implementation is
inefficient, because now every push or pop operation will require that all elements
currently in the stack be shifted one position in the array, for a cost of (n) if there
are n elements. The other choice is have the top element be at position n