Data Structures · Stack vs. Queue

Use Case 1: Stack — Undo Feature in Text Editors

📝 The text editor (Notepad / Word / VS Code)

untitled.txt — Undo ↶ Undo
just typed about to be deleted restored by Undo undone (disappears again)

📚 The action stack stack<string> — action history

↑ top = the last action
Undo only looks here
LIFO last-in-first-out
→ undo the newest first, then older ones

💬 Current step

💻 The C++ program (STL stack)

Yellow = current line. Every edit pushes a record; Undo simply pops the top and reverses it.

📄 The slide’s wording & key points

Problem: when a user types something and then clicks "Undo", the editor should undo the last action performed.
In a text editor (like Notepad, Word, or VS Code), when a user types something and then clicks "Undo", the editor should undo the last action performed.
Why Stack? Because a stack works on the Last-In, First-Out (LIFO) principle — the last action is always on top.
Because a stack works on the Last-In, First-Out (LIFO) principle.
1Every edit is pushed onto the stack — it records what that step did
2The last action sits on top — that is exactly what LIFO buys you
3Undo = pop the top + reverse it — undoing a delete puts the character back
4Empty stack = nothing left to undo — "Nothing to undo"
What if we used a queue? A queue dequeues from the front, so Undo would first revert the oldest action Type "Hello" instead of the one just performed, Delete 'o' — completely wrong. That is why Undo needs a stack.
Speed
Shortcuts: Space play/pause · ←→ step · R reset · E result