Mark As Completed Discussion

Adding More Elements: Stack Gets Taller

Now that we've pushed 10 into the stack, what happens when we push another element, say 20? Well, it will simply take its place on top of the last element we pushed, which is 10.

Visualizing a Stack with Multiple Elements

When we execute stack.push(20);, the stack's structure evolves as follows:

In this updated "box":
  • 10 now has a companion, 20, sitting right above it.
  • 20 is the last-added element, making it the first one to be removed if we perform a pop operation.

Notice how 20 took its position on top of 10? This sequence of stacking elements on top of each other is a vivid illustration of the Last In, First Out (LIFO) principle in action.