The Top Pointer
We initialize `top = -1`. This conceptually means "the array is completely empty". It's an index pointer, not a physical memory address.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
The most straightforward way to build a Stack is by wrapping a standard Array and using a single integer variable `top` to keep track of the highest index.
We initialize `top = -1`. This conceptually means "the array is completely empty". It's an index pointer, not a physical memory address.
To push, we first increment `top`, and then place the element at `array[top]`. We must check if `top == size - 1` to prevent buffer overflow.
To pop, we read `array[top]` and then decrement `top`. We don't even need to "delete" the data from the array—it will just get overwritten next time we push!