Once you understand what a stack is — a pile of items where you always add and remove from the same end — the next step is to learn the words we use to describe it. A stack has three important properties: its capacity, its size, and its top. These values can change every time we add an item to the stack or take one out.
Think of a stack like a tube of tennis balls. You can only push a ball in or pull a ball out from the open top. The same simple picture explains all three properties below.
The capacity of a stack is the *maximum* number of items it is allowed to hold — the total amount of room it has.
There are two kinds of stacks:
A helpful way to remember it: capacity is about the *container*, not about what is currently inside it. An empty tube and a full tube have the same capacity — the room never changes.
The size of a stack is how many items it is holding *right now*.
Unlike capacity, size keeps changing while the program runs:
For example, if a stack currently holds the numbers 1, 4, 5, 2, and 7, then its size is 5. If you add one more number, the size becomes 6. If you remove a number, it drops back to 4.
So you can think of it like this: **capacity is how much the stack *can* hold, and size is how much it *actually* holds at this moment.** Size can never be larger than capacity.
The top of a stack is the item that was inserted *last*.
Because a stack only lets you add and remove from one end, the most recently added item always sits at the very top of the pile. That top item is special — it is the only one you can see and the only one you can remove next.
Imagine numbers were pushed in this order: 1, then 4, then 5, then 2, then 7. The last one pushed in was 7, so 7 is the top of the stack. If you remove an item, 7 comes out first, and then 2 becomes the new top.
One more thing to remember: if the stack is empty, there is no top at all — there is simply nothing to point to.