
Do not confuse the top of
the stack with the last
index in the array.Array
elements with an index
higher than top are not on
the stack.
COMMON ERROR
TRAP
1048 CHAPTER 14 An Introduction to Data Structures
stack. The value of top will vary from –1 (when the stack is empty) to
STACK_SIZE – 1 (when the stack is full).
In the default constructor, coded at lines 11–15, we instantiate stack and
then set top to 1, which indicates that the stack is empty. When a client
program pushes the first Player onto the stack, top will be incremented, so
that the top of the stack will be the array element at index 0.
We coded the push method at lines 17–30. The push method ...