-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path00_StackOverview.cpp
More file actions
296 lines (240 loc) · 8.37 KB
/
Copy path00_StackOverview.cpp
File metadata and controls
296 lines (240 loc) · 8.37 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
================================================================================
STACK - OVERVIEW
================================================================================
LIFO (Last In, First Out) data structure.
Think of a stack of plates - you can only add/remove from the top.
Operations:
- push(x) : Add element to top - O(1)
- pop() : Remove top element - O(1)
- top/peek(): View top element - O(1)
- isEmpty() : Check if empty - O(1)
- size() : Get number of elements - O(1)
================================================================================
WHEN TO USE STACK
================================================================================
1. MATCHING PROBLEMS
- Valid parentheses, brackets
- HTML/XML tag matching
2. REVERSAL PROBLEMS
- Reverse a string/array
- Undo operations
3. EXPRESSION EVALUATION
- Infix to postfix conversion
- Evaluate postfix/prefix
- Calculator problems
4. MONOTONIC STACK (see 02_MonotonicStack.cpp)
- Next greater/smaller element
- Largest rectangle in histogram
- Daily temperatures
5. FUNCTION CALL SIMULATION
- DFS (recursion uses stack internally)
- Backtracking
================================================================================
C++ STL STACK
================================================================================
*/
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;
void stlStackDemo() {
stack<int> stk;
// Push elements
stk.push(10);
stk.push(20);
stk.push(30);
// Top element
cout << "Top: " << stk.top() << endl; // 30
// Size
cout << "Size: " << stk.size() << endl; // 3
// Pop
stk.pop();
cout << "After pop, Top: " << stk.top() << endl; // 20
// Check empty
cout << "Empty? " << (stk.empty() ? "Yes" : "No") << endl;
// Iterate (pop all)
cout << "All elements: ";
while (!stk.empty()) {
cout << stk.top() << " ";
stk.pop();
}
cout << endl;
}
/*
================================================================================
STACK IMPLEMENTATIONS
================================================================================
*/
// 1. ARRAY-BASED STACK (Fixed Size)
class ArrayStack {
private:
int* arr;
int topIdx;
int capacity;
public:
ArrayStack(int size = 100) {
capacity = size;
arr = new int[capacity];
topIdx = -1;
}
~ArrayStack() { delete[] arr; }
void push(int val) {
if (topIdx >= capacity - 1) {
cout << "Stack Overflow!" << endl;
return;
}
arr[++topIdx] = val;
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow!" << endl;
return -1;
}
return arr[topIdx--];
}
int top() {
if (isEmpty()) return -1;
return arr[topIdx];
}
bool isEmpty() { return topIdx == -1; }
int size() { return topIdx + 1; }
};
// 2. DYNAMIC ARRAY STACK (Vector-based)
class DynamicStack {
private:
vector<int> arr;
public:
void push(int val) {
arr.push_back(val);
}
int pop() {
if (isEmpty()) return -1;
int val = arr.back();
arr.pop_back();
return val;
}
int top() {
if (isEmpty()) return -1;
return arr.back();
}
bool isEmpty() { return arr.empty(); }
int size() { return arr.size(); }
};
// 3. LINKED LIST STACK
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class LinkedStack {
private:
ListNode* head;
int count;
public:
LinkedStack() : head(nullptr), count(0) {}
~LinkedStack() {
while (head) {
ListNode* temp = head;
head = head->next;
delete temp;
}
}
void push(int val) {
ListNode* node = new ListNode(val);
node->next = head;
head = node;
count++;
}
int pop() {
if (isEmpty()) return -1;
int val = head->val;
ListNode* temp = head;
head = head->next;
delete temp;
count--;
return val;
}
int top() {
if (isEmpty()) return -1;
return head->val;
}
bool isEmpty() { return head == nullptr; }
int size() { return count; }
};
/*
================================================================================
PATTERN CLASSIFICATION
================================================================================
1. BASIC STACK PROBLEMS (01_BasicProblems.cpp)
─────────────────────────────────────────────
- Valid Parentheses (LC 20)
- Min Stack (LC 155)
- Implement Queue using Stacks (LC 232)
- Implement Stack using Queues (LC 225)
- Simplify Path (LC 71)
- Decode String (LC 394)
- Remove All Adjacent Duplicates (LC 1047)
- Baseball Game (LC 682)
2. MONOTONIC STACK (02_MonotonicStack.cpp) ⭐
─────────────────────────────────────────────
- Next Greater Element (LC 496, 503)
- Daily Temperatures (LC 739)
- Stock Span (LC 901)
- Largest Rectangle in Histogram (LC 84)
- Trapping Rain Water (LC 42)
- Sum of Subarray Minimums (LC 907)
- Remove K Digits (LC 402)
3. EXPRESSION EVALUATION (03_ExpressionEvaluation.cpp)
─────────────────────────────────────────────────────
- Evaluate Reverse Polish Notation (LC 150)
- Basic Calculator (LC 224)
- Basic Calculator II (LC 227)
- Infix to Postfix Conversion
- Expression with Parentheses
================================================================================
*/
int main() {
cout << "=== Stack Overview ===" << endl << endl;
// STL Stack Demo
cout << "--- STL Stack Demo ---" << endl;
stlStackDemo();
// Array Stack Demo
cout << "\n--- Array Stack Demo ---" << endl;
ArrayStack arrStk(10);
arrStk.push(1);
arrStk.push(2);
arrStk.push(3);
cout << "Top: " << arrStk.top() << endl;
cout << "Pop: " << arrStk.pop() << endl;
cout << "Size: " << arrStk.size() << endl;
// Linked Stack Demo
cout << "\n--- Linked Stack Demo ---" << endl;
LinkedStack linkStk;
linkStk.push(10);
linkStk.push(20);
linkStk.push(30);
cout << "Top: " << linkStk.top() << endl;
cout << "Pop: " << linkStk.pop() << endl;
cout << "Size: " << linkStk.size() << endl;
return 0;
}
/*
================================================================================
SUMMARY
================================================================================
┌────────────────────┬────────────────┬────────────────────────────────────────┐
│ Implementation │ Pros │ Cons │
├────────────────────┼────────────────┼────────────────────────────────────────┤
│ Array (fixed) │ Simple, fast │ Fixed size, overflow possible │
│ Vector (dynamic) │ Auto-resize │ Occasional O(n) resize │
│ Linked List │ True O(1) │ Extra memory for pointers │
└────────────────────┴────────────────┴────────────────────────────────────────┘
In interviews, just use stack<int> stk; from STL!
COMMON MISTAKES:
1. Forgetting to check empty() before top()/pop()
2. Off-by-one errors in index-based implementation
3. Not handling stack overflow in fixed-size array
================================================================================
*/