-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA1_4.html
More file actions
112 lines (105 loc) · 7.08 KB
/
Copy pathDSA1_4.html
File metadata and controls
112 lines (105 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<article>
<h1>DSA I: Page 4 - Stacks: Ordered LIFO Processing</h1>
<section>
<h2>The Last-In-First-Out Data Structure</h2>
<p>A <strong>Stack</strong> is a linear data structure that follows the <strong>Last-In-First-Out (LIFO)</strong> principle. Think of it as a physical stack of plates: you can only add a plate to the top, and you can only take the top plate off. You cannot remove a plate from the middle without destabilizing the whole stack. This restricted access is not a limitation—it's exactly what makes stacks so powerful for managing ordered tasks and state.</p>
<pre><code class="language-python"># Simple Stack implementation using a Python list
stack = []
stack.append('A') # Push 'A'
stack.append('B') # Push 'B'
top = stack.pop() # Pop: returns 'B' (LIFO)
print(top) # Output: B</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://images.unsplash.com/photo-1629654297299-c8506221ca97?q=80&w=800&auto=format&fit=crop" alt="Abstract representation of a stack data structure">
</div>
</div>
</section>
<section>
<h2>1. Core Stack Operations</h2>
<p>A stack is defined by a very limited set of operations, ensuring that the LIFO order is always maintained.</p>
<ul>
<li><strong>Push:</strong> Add an element to the top of the stack.</li>
<li><strong>Pop:</strong> Remove the top element from the stack.</li>
<li><strong>Peek/Top:</strong> View the top element without removing it.</li>
<li><strong>isEmpty:</strong> Check if the stack contains any elements.</li>
</ul>
<pre><code class="language-python"># Standard Stack Operations
class Stack:
def __init__(self):
self.items = []
def push(self, item): self.items.append(item)
def pop(self): return self.items.pop() if not self.is_empty() else None
def peek(self): return self.items[-1] if not self.is_empty() else None
def is_empty(self): return len(self.items) == 0</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://images.unsplash.com/photo-1614741118887-7a4ee193a5fa?q=80&w=800&auto=format&fit=crop" alt="Algorithm performance visualization">
</div>
</div>
</section>
<section>
<h2>2. Real-World Stack Applications</h2>
<p>Because stacks perfectly manage "history" and "nesting," they are essential in many areas of computing:</p>
<ul>
<li><strong>Call Stack:</strong> Every time a function is called in your code, its state is "pushed" onto the call stack. When it finishes, it's "popped" off, returning control to the calling function.</li>
<li><strong>Undo/Redo:</strong> Text editors use a stack to keep track of your actions; "Undo" pops the last action off the stack.</li>
<li><strong>Expression Evaluation:</strong> Parsing mathematical expressions (e.g., checking for balanced parentheses).</li>
<li><strong>Backtracking:</strong> Algorithms like finding a path through a maze use a stack to keep track of visited intersections.</li>
</ul>
<pre><code class="language-python"># Example: Using a stack to check for balanced parentheses
def is_balanced(expression):
stack = []
for char in expression:
if char == '(': stack.append(char)
elif char == ')':
if not stack: return False
stack.pop()
return len(stack) == 0</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://images.unsplash.com/photo-1531403009284-440f080d1e12?q=80&w=800&auto=format&fit=crop" alt="Logic and mathematics visualization">
</div>
</div>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master stacks with these three videos:</p>
<div style="display: flex; gap: 20px; flex-wrap: wrap; margin-top: 20px;">
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>1. Stacks Explained Simply</strong><br>
<a href="https://www.youtube.com/watch?v=BBpAmxU_NQo" target="_blank">Watch on YouTube →</a>
<p><small>The LIFO principle and basic operations.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. The Call Stack in Programming</strong><br>
<a href="https://www.youtube.com/watch?v=8hly31xKli0" target="_blank">Watch on YouTube →</a>
<p><small>How functions manage execution state.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Stack Implementation</strong><br>
<a href="https://www.youtube.com/watch?v=zg9ih6SVACc" target="_blank">Watch on YouTube →</a>
<p><small>How to implement a stack in code.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The Cafeteria Tray Stack</h2>
<p>Think of a <strong>Stack</strong> like a <strong>Stack of Trays in a Cafeteria</strong>. A new tray is placed on top of the stack. When you need a tray, you *must* take the one from the top—you can't pull one from the bottom without making a mess. This is exactly how the <strong>Call Stack</strong> works: you must finish the most recently started function (top of the stack) before you can return to the function that called it (the one underneath).</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Stack_(abstract_data_type)" target="_blank">Wikipedia: Stack Overview</a></li>
<li><a href="https://www.geeksforgeeks.org/stack-data-structure/" target="_blank">GeeksforGeeks: Stack Basics</a></li>
<li><a href="https://www.w3schools.com/dsa/dsa_stacks.php" target="_blank">W3Schools: DSA Stacks</a></li>
<li><a href="https://visualgo.net/en/stack" target="_blank">Tool: Stack Visualization</a></li>
</ul>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="DSA1_3.html" style="text-decoration: none; color: #6c757d;">← Previous: Linked Lists</a>
<a href="#" data-file="DSA1_5.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Queues →</a>
</div>
</footer>
</article>