Skip to main content

Command Palette

Search for a command to run...

Understanding Stacks

Updated
1 min read
Understanding Stacks
B

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

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:

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/.