From 030887c7dff96aa2cbafe89ad01931a8af371483 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Tue, 6 Oct 2026 12:58:52 -0400 Subject: [PATCH] docs(docs): add detailed stack overview with operation examples and SVG illustrations --- .../(01-linear-structures)/stack/index.mdx | 57 ++++++++++++++ .../docs/public/stack-operations-sequence.svg | 77 +++++++++++++++++++ apps/docs/public/stack-plates-metaphor.svg | 62 +++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 apps/docs/public/stack-operations-sequence.svg create mode 100644 apps/docs/public/stack-plates-metaphor.svg diff --git a/apps/docs/content/(01-linear-structures)/stack/index.mdx b/apps/docs/content/(01-linear-structures)/stack/index.mdx index 2b58b86..f77b9f9 100644 --- a/apps/docs/content/(01-linear-structures)/stack/index.mdx +++ b/apps/docs/content/(01-linear-structures)/stack/index.mdx @@ -3,3 +3,60 @@ title: Stack description: 'Items are added and removed from one end, so the last item added is the first item removed.' hidden: true --- + +A stack works like a stack of plates in a cafeteria. You add new plates on top. When you need a plate, you take from the top. The last plate you added is the first one you remove. + +This is **Last-In-First-Out (LIFO)**. The most recent item goes out first. + +![](../../../public/stack-plates-metaphor.svg) + +## Core Stack Operations + +A stack has two fundamental operations: + +- **Push**: add an item to the top of the stack +- **Pop**: remove the item at the top of the stack + +Some stacks have additional operations: + +- **Peek**: get the item at the top of the stack without removing it +- **IsEmpty**: check if the stack is empty + +![](../../../public/stack-operations-sequence.svg) + +```python +# Using Python list as a stack +stack = [] + +# Push elements +stack.append(10) +stack.append(20) +stack.append(30) +# Stack: [10, 20, 30] (30 is on top) + +# Pop element +top = stack.pop() # Returns 30 +# Stack: [10, 20] + +# Peek at top +if stack: + top_element = stack[-1] # Returns 20 without removing +``` + +Python lists provide `append()` for pushing and `pop()` for popping. The `peek()` method is not built-in, but you can implement it yourself. + +## Last in, First out + +The defining characteristic of a stack is LIFO order. Whatever you put in last comes out first. This is different from a queue, which is First-In-First-Out (FIFO). + +Add items: A, B, C + +- Stack: [A, B, C] (C on top) + +Remove items: + +- First pop: C +- Second pop: B +- Third pop: A + +The order reverses. Items come out in the opposite order they went in. diff --git a/apps/docs/public/stack-operations-sequence.svg b/apps/docs/public/stack-operations-sequence.svg new file mode 100644 index 0000000..af82b4f --- /dev/null +++ b/apps/docs/public/stack-operations-sequence.svg @@ -0,0 +1,77 @@ + + + + + + + + + + Core Stack Operations + + PUSH + Add to top + + + 10 + + 20 + + 30 + + New + ← TOP + + + POP + Remove from top + + + 10 + + 20 + + 30 + + Removed + + 30 + Returns: + ← TOP + + + PEEK + View top (no remove) + + + 10 + + 20 + + 30 + + View only + + 30 + Returns: + ← TOP + Stack unchanged + + + isEmpty + Check if empty + + Empty + + true + Returns: + + + + All operations work on the TOP of the stack + • PUSH adds element to top + • POP removes and returns top + • PEEK returns top without removing + • isEmpty checks size + + \ No newline at end of file diff --git a/apps/docs/public/stack-plates-metaphor.svg b/apps/docs/public/stack-plates-metaphor.svg new file mode 100644 index 0000000..fcbfac3 --- /dev/null +++ b/apps/docs/public/stack-plates-metaphor.svg @@ -0,0 +1,62 @@ + + + + + + + + + + + + + Stack of Plates - LIFO (Last In, First Out) + + Adding Plates (Push) + + + + + Plate 1 + + + + Plate 2 + + + + Plate 3 + + Add on top + TOP ➜ + + + + LIFO + Last plate added + is first removed + + + Removing Plates (Pop) + + + + Plate 1 + + + + Plate 2 + + + + Plate 3 + + Remove from top + TOP ➜ + + + + Stack Behavior: The last plate you add is the first plate you take. + You can only add or remove plates from the TOP of the stack. + + \ No newline at end of file