previous | start

Implementation

We could use an array (with elements of the generic element type E) to store the elements of the stack.

But an array is fixed size, so it doesn't actually grow or shrink as items are inserted or deleted.

We could keep track of two things:

  1. Actual size of the array - capacity.
  2. The number of items logically inserted in the array - sz.

Then,

This can work, but the major problem is the size of the array. The push method cannot do its job if the array is full!

This can be overcome by using an ArrayList instead of an ordinary array since an ArrayList grows as needed when elements are added.



previous | start