Module 1: Stacks (The Pringles Can)
๐ Module 1: Stacks
Course ID: DSA-105
Subject: The Pringles Can
A Stack is a data structure where you can only add or remove items from the Top. It follows the LIFO rule: Last-In, First-Out.
๐๏ธ Step 1: LIFO Logic
๐งฉ The Analogy: The Pringles Can
- You put the first chip in the bottom of the can.
- You put the last chip at the very top.
- If you want to eat a chip, you MUST eat the Top one first.
- To get to the bottom chip, you have to remove every other chip above it!
๐๏ธ Step 2: The Two Operations
- PUSH: Putting a new item on the top of the stack.
- POP: Taking the top item off the stack.
- Bonus: PEEK: Looking at the top item without removing it.
๐๏ธ Step 3: Why do we use it?
- Undo Button: Every time you type, your actions are โPushedโ onto a stack. When you hit Ctrl+Z, the last action is โPopped.โ
- The Call Stack: This is how your computer remembers where it is. If Function A calls Function B, the computer โPushesโ A onto a stack. When B is finished, it โPopsโ A to remember where to return.
๐ฅ Module 1 Review
- LIFO: Last-In, First-Out.
- Push/Pop: The only way to interact with the data.
- Restricted: You canโt see or touch anything in the middle of the stack.
:::tip Slow Learner Note A stack is just a regular list where we choose to only use one end! You can implement a stack in Python using a list with just .append() and .pop(). :::