docs(docs): add detailed stack overview with operation examples and SVG illustrations

This commit is contained in:
Prad Nukala committed 2026-10-06 12:58:52 -04:00
1 parent 733c71d364
commit 030887c7df
3 files changed
+196

No files matched your search

@@ -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.' description: 'Items are added and removed from one end, so the last item added is the first item removed.'
hidden: true 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.
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Pixodesk SVG (https://pixodesk.com) -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 400" text-rendering="geometricPrecision" shape-rendering="geometricPrecision">
<defs>
<marker id="arrowhead" refX="9" refY="3" markerHeight="10" markerWidth="10" orient="auto">
<path fill="#e74c3c" d="M0,0L10,3L0,6"/>
</marker>
</defs>
<rect fill="#f8f9fa" rx="10" ry="0" width="900" height="400"/>
<text fill="#2c3e50" text-anchor="middle" font-family="Arial, sans-serif" font-size="22" font-weight="700" xml:space="preserve" x="450" y="30" data-gn-el-type="text" style="white-space:pre"><tspan>Core Stack Operations</tspan></text>
<g id="push-operation">
<text fill="#27ae60" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" font-weight="700" xml:space="preserve" x="90" y="75" data-gn-el-type="text" style="white-space:pre"><tspan>PUSH</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" x="50" y="95" data-gn-el-type="text" style="white-space:pre"><tspan>Add to top</tspan></text>
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="5" ry="0" x="40" y="110" width="100" height="200"/>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="50" y="280" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="90" y="297" data-gn-el-type="text" style="white-space:pre"><tspan>10</tspan></text>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="50" y="250" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="90" y="267" data-gn-el-type="text" style="white-space:pre"><tspan>20</tspan></text>
<rect fill="#27ae60" stroke="#229954" stroke-width="2" rx="3" ry="0" x="50" y="200" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="90" y="217" data-gn-el-type="text" style="white-space:pre"><tspan>30</tspan></text>
<path fill="none" stroke="#27ae60" stroke-width="2" marker-end="url(#arrowhead)" d="M30,180L30,210"/>
<text fill="#27ae60" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" x="10" y="170" data-gn-el-type="text" style="white-space:pre"><tspan>New</tspan></text>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" x="140" y="215" data-gn-el-type="text" style="white-space:pre"><tspan>← TOP</tspan></text>
</g>
<g id="pop-operation">
<text fill="#e74c3c" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" font-weight="700" xml:space="preserve" x="270" y="75" data-gn-el-type="text" style="white-space:pre"><tspan>POP</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" x="215" y="95" data-gn-el-type="text" style="white-space:pre"><tspan>Remove from top</tspan></text>
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="5" ry="0" x="220" y="110" width="100" height="200"/>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="230" y="280" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="270" y="297" data-gn-el-type="text" style="white-space:pre"><tspan>10</tspan></text>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="230" y="250" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="270" y="267" data-gn-el-type="text" style="white-space:pre"><tspan>20</tspan></text>
<rect fill="#e74c3c" stroke="#c0392b" stroke-width="2" opacity="0.5" rx="3" ry="0" x="230" y="180" width="80" height="25"/>
<text fill="#ffffff" opacity="0.5" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="270" y="197" data-gn-el-type="text" style="white-space:pre"><tspan>30</tspan></text>
<path fill="none" stroke="#e74c3c" stroke-width="2" marker-end="url(#arrowhead)" d="M330,185L360,150"/>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" transform="translate(-3, 7)" x="365" y="145" data-gn-el-type="text" style="white-space:pre"><tspan>Removed</tspan></text>
<rect fill="#e74c3c" stroke="#c0392b" stroke-width="2" rx="3" ry="0" x="350" y="115" width="60" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="380" y="132" data-gn-el-type="text" style="white-space:pre"><tspan>30</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="10" xml:space="preserve" x="350" y="108" data-gn-el-type="text" style="white-space:pre"><tspan>Returns:</tspan></text>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" x="320" y="265" data-gn-el-type="text" style="white-space:pre"><tspan>← TOP</tspan></text>
</g>
<g id="peek-operation">
<text fill="#f39c12" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" font-weight="700" xml:space="preserve" x="520" y="75" data-gn-el-type="text" style="white-space:pre"><tspan>PEEK</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" x="465" y="95" data-gn-el-type="text" style="white-space:pre"><tspan>View top (no remove)</tspan></text>
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="5" ry="0" x="470" y="110" width="100" height="200"/>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="480" y="280" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="520" y="297" data-gn-el-type="text" style="white-space:pre"><tspan>10</tspan></text>
<rect fill="#3498db" stroke="#2980b9" stroke-width="2" rx="3" ry="0" x="480" y="250" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="520" y="267" data-gn-el-type="text" style="white-space:pre"><tspan>20</tspan></text>
<rect fill="#f39c12" stroke="#e67e22" stroke-width="3" rx="3" ry="0" x="480" y="220" width="80" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="520" y="237" data-gn-el-type="text" style="white-space:pre"><tspan>30</tspan></text>
<path fill="none" stroke="#f39c12" stroke-width="2" stroke-dasharray="4 4" d="M580,232L620,190"/>
<text fill="#f39c12" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" transform="translate(-2, 12)" x="625" y="185" data-gn-el-type="text" style="white-space:pre"><tspan>View only</tspan></text>
<rect fill="#f39c12" stroke="#e67e22" stroke-width="2" rx="3" ry="0" x="610" y="160" width="60" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" x="640" y="177" data-gn-el-type="text" style="white-space:pre"><tspan>30</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="10" xml:space="preserve" x="610" y="153" data-gn-el-type="text" style="white-space:pre"><tspan>Returns:</tspan></text>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="11" font-weight="700" xml:space="preserve" x="570" y="235" data-gn-el-type="text" style="white-space:pre"><tspan>← TOP</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="10" xml:space="preserve" transform="translate(8, 0)" x="470" y="190" data-gn-el-type="text" style="white-space:pre"><tspan>Stack unchanged</tspan></text>
</g>
<g id="isEmpty-operation">
<text fill="#9b59b6" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" font-weight="700" xml:space="preserve" x="780" y="75" data-gn-el-type="text" style="white-space:pre"><tspan>isEmpty</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" x="730" y="95" data-gn-el-type="text" style="white-space:pre"><tspan>Check if empty</tspan></text>
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="5" ry="0" x="730" y="110" width="100" height="200"/>
<text fill="#bdc3c7" text-anchor="middle" font-family="Arial, sans-serif" font-style="italic" font-size="14" xml:space="preserve" x="780" y="215" data-gn-el-type="text" style="white-space:pre"><tspan>Empty</tspan></text>
<rect fill="#9b59b6" stroke="#8e44ad" stroke-width="2" transform="translate(84.1444, -90.4029)" rx="3" ry="0" x="750" y="250" width="60" height="25"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" font-weight="700" xml:space="preserve" transform="translate(84.1444, -90.4029)" x="780" y="267" data-gn-el-type="text" style="white-space:pre"><tspan>true</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="10" xml:space="preserve" transform="translate(86.9966, -89.3466)" x="750" y="243" data-gn-el-type="text" style="white-space:pre"><tspan>Returns:</tspan></text>
</g>
<g id="explanation">
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="8" ry="0" x="40" y="330" width="820" height="55"/>
<text fill="#2c3e50" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="450" y="350" data-gn-el-type="text" style="white-space:pre"><tspan>All operations work on the TOP of the stack</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" transform="translate(-47, 0)" x="100" y="370" data-gn-el-type="text" style="white-space:pre"><tspan>• PUSH adds element to top</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" transform="translate(-47, 0)" x="300" y="370" data-gn-el-type="text" style="white-space:pre"><tspan>• POP removes and returns top</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" transform="translate(-47, 0)" x="520" y="370" data-gn-el-type="text" style="white-space:pre"><tspan>• PEEK returns top without removing</tspan></text>
<text fill="#7f8c8d" font-family="Arial, sans-serif" font-size="12" xml:space="preserve" transform="translate(-47, 0)" x="760" y="370" data-gn-el-type="text" style="white-space:pre"><tspan>• isEmpty checks size</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 11 KiB

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Pixodesk SVG (https://pixodesk.com) -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 400" text-rendering="geometricPrecision" shape-rendering="geometricPrecision">
<defs>
<marker id="arrowhead" refX="9" refY="3" markerHeight="10" markerWidth="10" orient="auto">
<path fill="#e74c3c" d="M0,0L10,3L0,6"/>
</marker>
<marker id="arrowhead-green" refX="9" refY="3" markerHeight="10" markerWidth="10" orient="auto">
<path fill="#27ae60" d="M0,0L10,3L0,6"/>
</marker>
</defs>
<rect fill="#f8f9fa" rx="10" ry="0" width="800" height="399.6807"/>
<text fill="#2c3e50" text-anchor="middle" font-family="Arial, sans-serif" font-size="22" font-weight="700" xml:space="preserve" x="400" y="35" data-gn-el-type="text" style="white-space:pre"><tspan>Stack of Plates - LIFO (Last In, First Out)</tspan></text>
<g id="push-operation" transform="translate(0, -105)">
<text fill="#27ae60" text-anchor="middle" font-family="Arial, sans-serif" font-size="18" font-weight="700" xml:space="preserve" transform="translate(0, 102)" x="200" y="80" data-gn-el-type="text" style="white-space:pre"><tspan>Adding Plates (Push)</tspan></text>
<rect fill="#7f8c8d" rx="3" ry="0" x="120" y="380" width="160" height="15" style="display:none"/>
<ellipse fill="#3498db" stroke="#2980b9" stroke-width="2" cx="200" cy="370" rx="70" ry="12"/>
<rect fill="#3498db" x="130" y="370" width="140" height="25"/>
<ellipse fill="#2980b9" stroke="#2471a3" stroke-width="2" cx="200" cy="395" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="200" y="387" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 1</tspan></text>
<ellipse fill="#3498db" stroke="#2980b9" stroke-width="2" cx="200" cy="330" rx="70" ry="12"/>
<rect fill="#3498db" x="130" y="330" width="140" height="25"/>
<ellipse fill="#2980b9" stroke="#2471a3" stroke-width="2" cx="200" cy="355" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="200" y="347" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 2</tspan></text>
<ellipse fill="#27ae60" stroke="#229954" stroke-width="2" cx="200" cy="290" rx="70" ry="12"/>
<rect fill="#27ae60" x="130" y="290" width="140" height="25"/>
<ellipse fill="#229954" stroke="#1e8449" stroke-width="2" cx="200" cy="315" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="200" y="307" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 3</tspan></text>
<path fill="none" stroke="#27ae60" stroke-width="3" marker-end="url(#arrowhead-green)" d="M200,250L200,275"/>
<text fill="#27ae60" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="230" y="265" data-gn-el-type="text" style="white-space:pre"><tspan>Add on top</tspan></text>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="85" y="307" data-gn-el-type="text" style="white-space:pre"><tspan>TOP ➜</tspan></text>
</g>
<g id="lifo-flow" transform="translate(0, -105)">
<path fill="none" stroke="#95a5a6" stroke-width="3" marker-end="url(#arrowhead)" d="M320,300L480,300"/>
<text fill="#e74c3c" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" font-weight="700" xml:space="preserve" x="400" y="290" data-gn-el-type="text" style="white-space:pre"><tspan>LIFO</tspan></text>
<text fill="#7f8c8d" text-anchor="middle" font-family="Arial, sans-serif" font-size="13" xml:space="preserve" x="400" y="330" data-gn-el-type="text" style="white-space:pre"><tspan>Last plate added</tspan></text>
<text fill="#7f8c8d" text-anchor="middle" font-family="Arial, sans-serif" font-size="13" xml:space="preserve" x="400" y="348" data-gn-el-type="text" style="white-space:pre"><tspan>is first removed</tspan></text>
</g>
<g id="pop-operation" transform="translate(0, -105)">
<text fill="#e74c3c" text-anchor="middle" font-family="Arial, sans-serif" font-size="18" font-weight="700" xml:space="preserve" transform="translate(0, 101)" x="600" y="80" data-gn-el-type="text" style="white-space:pre"><tspan>Removing Plates (Pop)</tspan></text>
<ellipse fill="#3498db" stroke="#2980b9" stroke-width="2" cx="600" cy="370" rx="70" ry="12"/>
<rect fill="#3498db" x="530" y="370" width="140" height="25"/>
<ellipse fill="#2980b9" stroke="#2471a3" stroke-width="2" cx="600" cy="395" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="600" y="387" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 1</tspan></text>
<ellipse fill="#3498db" stroke="#2980b9" stroke-width="2" cx="600" cy="330" rx="70" ry="12"/>
<rect fill="#3498db" x="530" y="330" width="140" height="25"/>
<ellipse fill="#2980b9" stroke="#2471a3" stroke-width="2" cx="600" cy="355" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="600" y="347" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 2</tspan></text>
<ellipse fill="#e74c3c" stroke="#c0392b" stroke-width="2" cx="600" cy="230" rx="70" ry="12"/>
<rect fill="#e74c3c" x="530" y="230" width="140" height="25"/>
<ellipse fill="#c0392b" stroke="#a93226" stroke-width="2" cx="600" cy="255" rx="70" ry="12"/>
<text fill="#ffffff" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="600" y="247" data-gn-el-type="text" style="white-space:pre"><tspan>Plate 3</tspan></text>
<path fill="none" stroke="#e74c3c" stroke-width="3" transform="translate(0, 63)" marker-end="url(#arrowhead)" d="M600,250.6757L600,207.2249"/>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="670" y="245" data-gn-el-type="text" style="white-space:pre"><tspan>Remove from top</tspan></text>
<text fill="#e74c3c" font-family="Arial, sans-serif" font-size="14" font-weight="700" xml:space="preserve" x="485" y="347" data-gn-el-type="text" style="white-space:pre"><tspan>TOP ➜</tspan></text>
</g>
<g id="explanation" transform="translate(0, -105)">
<rect fill="#ffffff" stroke="#bdc3c7" stroke-width="2" rx="8" ry="0" x="50" y="430" width="700" height="55"/>
<text fill="#2c3e50" text-anchor="middle" font-family="Arial, sans-serif" font-size="15" font-weight="700" xml:space="preserve" x="400" y="453" data-gn-el-type="text" style="white-space:pre"><tspan>Stack Behavior: The last plate you add is the first plate you take.</tspan></text>
<text fill="#7f8c8d" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" xml:space="preserve" x="400" y="473" data-gn-el-type="text" style="white-space:pre"><tspan>You can only add or remove plates from the TOP of the stack.</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 7.1 KiB