Understanding Stacks

Brett RowberryBrett Rowberry
1 min read

In computer science, a stack is what you think it is. A pile. There are two important restrictions:

  1. Only add to the top

  2. Only remove from the top

To illustrate, let’s track how we get dressed using a stack. At first, the stack is empty. Let’s add a shirt, a sweater, and a coat. Our stack now looks like this:

flowchart TD
coat ~~~ sweater ~~~ shirt

Now, we want to go swimming. In real life, and in a stack, we can’t remove the shirt first since the sweater and coat are in the way. The stack we built tells us the order in which to remove the clothes: coat, sweater, shirt.

The only thing we can access is the last thing we put in. Therefore, we say that a stack is last-in, first-out (LIFO). We could also call it first-in, last-out (FILO), but nobody says that.

You can read more at https://www.geeksforgeeks.org/dsa/applications-advantages-and-disadvantages-of-stack/.

0
Subscribe to my newsletter

Read articles from Brett Rowberry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Brett Rowberry
Brett Rowberry

I like programming (at work) and learning for fun. You'll often find me cooking and working on my house in my spare time.