← All Lessons Lesson 3 / 76
Lesson 3

Key Properties of a Stack: Capacity, Size, and Top

Key Properties of a Stack

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.

Capacity

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 bounded stack has a fixed, pre-decided capacity. It can only hold up to a set number of items, just like a tennis-ball tube that physically fits, say, 7 balls and not one more.
  • An unbounded stack has no fixed limit. In theory it can keep growing as long as the computer has free memory to store the new items. The only real limit is how much memory the machine has.

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.

Size

The size of a stack is how many items it is holding *right now*.

Unlike capacity, size keeps changing while the program runs:

  • Every time you add an item, the size goes up by one.
  • Every time you remove an item, the size goes down by one.

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.

Top

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.

Quick recap

  • Capacity → the most items the stack can ever hold (fixed for bounded, memory-limited for unbounded).
  • Size → how many items it holds right now (changes as you add or remove).
  • Top → the last item added, and the next one that will be removed.
Key Properties of a Stack: Capacity, Size, and Top
Diagram — click to zoom (scroll / drag to pan)