Repository navigation
Expand file tree
/
Copy pathstack.html
More file actions
1596 lines (1412 loc) · 72.8 KB
/
Copy pathstack.html
File metadata and controls
1596 lines (1412 loc) · 72.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stack - Complete DSA Visual Guide (C++)</title>
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;600;700&family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--orange-light: #FFB84D;
--orange-main: #FFA600;
--orange-dark: #FF8C42;
--orange-bright: #FF6B35;
--burnt-orange: #D84315;
--deep-orange: #BF360C;
--golden: #FFD700;
--amber: #FFC107;
--brown: #3E2723;
}
body {
font-family: 'Roboto', sans-serif;
background: linear-gradient(135deg, var(--deep-orange) 0%, var(--burnt-orange) 30%, var(--orange-dark) 60%, var(--orange-main) 100%);
padding: 40px 20px;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(191, 54, 12, 0.4);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, var(--deep-orange) 0%, var(--burnt-orange) 50%, var(--orange-bright) 100%);
color: white;
padding: 80px 40px;
text-align: center;
position: relative;
overflow: hidden;
}
.header::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background:
repeating-linear-gradient(
0deg,
transparent,
transparent 10px,
rgba(255,255,255,0.05) 10px,
rgba(255,255,255,0.05) 20px
);
pointer-events: none;
}
.header h1 {
font-family: 'Oswald', sans-serif;
font-size: 5em;
font-weight: 700;
margin-bottom: 15px;
letter-spacing: 15px;
text-shadow: 4px 4px 8px rgba(0,0,0,0.5);
position: relative;
}
.header p {
font-family: 'Oswald', sans-serif;
font-size: 1.5em;
letter-spacing: 5px;
font-weight: 600;
opacity: 0.95;
}
.section {
padding: 50px 60px;
border-bottom: 3px solid var(--orange-light);
}
.section:nth-child(even) {
background: linear-gradient(to right, #FFF8F0 0%, #ffffff 100%);
}
.section:last-child {
border-bottom: none;
}
.section-title {
font-family: 'Oswald', sans-serif;
font-size: 2.3em;
font-weight: 700;
margin-bottom: 30px;
color: var(--burnt-orange);
display: flex;
align-items: center;
gap: 20px;
}
.section-number {
background: linear-gradient(135deg, var(--orange-bright) 0%, var(--orange-main) 50%, var(--golden) 100%);
color: white;
width: 65px;
height: 65px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.3em;
font-weight: 700;
box-shadow: 0 5px 20px rgba(255, 107, 53, 0.5);
border: 4px solid var(--burnt-orange);
}
h3 {
color: var(--burnt-orange);
font-family: 'Oswald', sans-serif;
margin: 30px 0 20px;
font-size: 1.6em;
}
p {
color: var(--brown);
line-height: 2;
margin-bottom: 18px;
}
.stack-visual {
display: inline-flex;
flex-direction: column-reverse;
margin: 30px 0;
box-shadow: 0 8px 25px rgba(191, 54, 12, 0.3);
border-radius: 12px;
overflow: hidden;
}
.stack-item {
background: linear-gradient(to bottom, var(--orange-light) 0%, var(--orange-main) 100%);
border: 4px solid var(--burnt-orange);
border-bottom: none;
padding: 25px 60px;
text-align: center;
min-width: 200px;
position: relative;
}
.stack-item:first-child {
border-bottom: 4px solid var(--burnt-orange);
border-radius: 0 0 12px 12px;
}
.stack-item:last-child {
border-radius: 12px 12px 0 0;
background: linear-gradient(to bottom, var(--golden) 0%, var(--amber) 100%);
box-shadow: 0 -4px 15px rgba(255, 193, 7, 0.5);
}
.stack-item:last-child::after {
content: '← TOP';
position: absolute;
right: -80px;
top: 50%;
transform: translateY(-50%);
font-weight: 700;
color: var(--burnt-orange);
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
}
.stack-value {
font-weight: 900;
color: white;
font-size: 1.8em;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.stack-label {
font-size: 0.8em;
color: var(--deep-orange);
margin-top: 5px;
font-weight: 600;
}
.code-snippet {
background: var(--brown);
color: var(--orange-light);
padding: 28px;
border-radius: 14px;
font-family: 'Consolas', 'Monaco', monospace;
margin: 28px 0;
overflow-x: auto;
font-size: 0.95em;
border: 4px solid var(--burnt-orange);
box-shadow: 0 6px 20px rgba(62, 39, 35, 0.3);
position: relative;
}
.code-snippet::before {
content: 'C++';
position: absolute;
top: 10px;
right: 15px;
font-size: 0.75em;
color: var(--orange-main);
font-weight: 700;
font-family: 'Oswald', sans-serif;
}
.code-keyword { color: #FF8C42; }
.code-type { color: #FFB84D; }
.code-string { color: #A5D6A7; }
.code-comment { color: #B0BEC5; font-style: italic; }
.code-number { color: #CE93D8; }
.comparison-table {
width: 100%;
border-collapse: collapse;
margin: 28px 0;
box-shadow: 0 6px 25px rgba(191, 54, 12, 0.2);
border-radius: 14px;
overflow: hidden;
}
.comparison-table th {
background: linear-gradient(135deg, var(--burnt-orange) 0%, var(--orange-bright) 100%);
color: white;
padding: 22px;
text-align: left;
font-family: 'Oswald', sans-serif;
font-weight: 700;
font-size: 1.1em;
}
.comparison-table td {
padding: 20px 22px;
border-bottom: 2px solid var(--orange-light);
color: var(--brown);
}
.comparison-table tr:last-child td {
border-bottom: none;
}
.comparison-table tr:nth-child(even) {
background: linear-gradient(to right, #FFF8F0 0%, #ffffff 100%);
}
.type-card, .technique-card {
background: linear-gradient(135deg, #FFF3E0 0%, #FFF8F0 100%);
border-left: 6px solid var(--orange-bright);
padding: 30px;
margin: 25px 0;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(255, 107, 53, 0.15);
}
.type-card h3, .type-card h4, .technique-card h4 {
color: var(--burnt-orange);
margin-bottom: 15px;
font-family: 'Oswald', sans-serif;
}
.operation-grid, .problem-grid, .use-case-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
gap: 25px;
margin: 30px 0;
}
.operation-card, .problem-item, .use-case {
background: linear-gradient(135deg, #ffffff 0%, #FFF3E0 100%);
border: 4px solid var(--orange-main);
border-radius: 16px;
padding: 30px;
transition: all 0.3s ease;
box-shadow: 0 4px 12px rgba(255, 140, 66, 0.15);
}
.operation-card:hover, .problem-item:hover, .use-case:hover {
transform: translateY(-10px);
box-shadow: 0 12px 30px rgba(255, 107, 53, 0.3);
border-color: var(--orange-bright);
}
.operation-card h4 {
font-family: 'Oswald', sans-serif;
color: var(--burnt-orange);
margin-bottom: 18px;
font-size: 1.4em;
}
.operation-card ul {
list-style: none;
padding-left: 0;
}
.operation-card li {
padding: 10px 0;
position: relative;
padding-left: 30px;
color: var(--brown);
}
.operation-card li:before {
content: "▸";
position: absolute;
left: 0;
color: var(--orange-main);
font-weight: 700;
font-size: 1.4em;
}
.complexity-box {
background: linear-gradient(135deg, #FFF3E0 0%, #ffffff 100%);
border: 4px solid var(--orange-main);
padding: 30px 35px;
margin: 25px 0;
border-radius: 16px;
box-shadow: 0 6px 20px rgba(255, 140, 66, 0.15);
}
.complexity-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 0;
border-bottom: 2px solid var(--orange-light);
}
.complexity-item:last-child {
border-bottom: none;
}
.complexity-label {
font-weight: 600;
color: var(--brown);
font-size: 1.05em;
}
.complexity-value {
font-family: 'Oswald', monospace;
background: linear-gradient(135deg, var(--orange-bright) 0%, var(--orange-main) 100%);
color: white;
padding: 10px 25px;
border-radius: 10px;
font-weight: 700;
box-shadow: 0 4px 12px rgba(255, 107, 53, 0.3);
}
.tip-box {
background: linear-gradient(135deg, #FFFDE7 0%, #FFF9C4 100%);
border-left: 6px solid var(--golden);
padding: 25px 30px;
margin: 25px 0;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(255, 215, 0, 0.2);
}
.tip-box strong {
color: #F57C00;
font-size: 1.2em;
font-family: 'Oswald', sans-serif;
}
.mistake-card {
background: linear-gradient(135deg, #FFEBEE 0%, #FFF5F5 100%);
border-left: 6px solid #D32F2F;
padding: 30px;
margin: 25px 0;
border-radius: 12px;
box-shadow: 0 4px 18px rgba(211, 47, 47, 0.2);
}
.mistake-card h4 {
color: #C62828;
margin-bottom: 16px;
font-family: 'Oswald', sans-serif;
font-size: 1.4em;
}
.mistake-card p {
color: #5D4037;
line-height: 1.9;
}
.problem-difficulty {
display: inline-block;
padding: 7px 18px;
border-radius: 25px;
font-size: 0.8em;
font-weight: 700;
margin-bottom: 14px;
font-family: 'Oswald', sans-serif;
}
.difficulty-easy { background: #4CAF50; color: white; }
.difficulty-medium { background: #FF9800; color: white; }
.difficulty-hard { background: #F44336; color: white; }
.problem-title {
font-weight: 700;
color: var(--burnt-orange);
font-size: 1.2em;
margin-bottom: 12px;
font-family: 'Oswald', sans-serif;
}
.problem-approach {
color: var(--brown);
font-size: 0.95em;
line-height: 1.7;
}
.use-case {
text-align: center;
}
.use-case-icon {
font-size: 3.8em;
margin-bottom: 18px;
}
.use-case-label {
font-weight: 700;
font-family: 'Oswald', sans-serif;
color: var(--burnt-orange);
font-size: 1.1em;
}
.summary-box {
background: linear-gradient(135deg, var(--deep-orange) 0%, var(--burnt-orange) 50%, var(--orange-bright) 100%);
color: white;
padding: 55px;
border-radius: 22px;
margin: 35px 0;
box-shadow: 0 12px 35px rgba(191, 54, 12, 0.5);
}
.summary-box h3 {
font-family: 'Oswald', sans-serif;
font-size: 2.4em;
margin-bottom: 30px;
color: white;
}
.summary-box ul {
list-style: none;
padding: 0;
}
.summary-box li {
padding: 16px 0;
font-size: 1.15em;
position: relative;
padding-left: 45px;
line-height: 1.8;
}
.summary-box li:before {
content: "✓";
position: absolute;
left: 0;
font-weight: 700;
font-size: 1.6em;
color: var(--golden);
}
.decision-tree {
background: white;
padding: 35px;
border-radius: 18px;
margin: 30px 0;
border: 4px solid var(--orange-main);
box-shadow: 0 6px 22px rgba(255, 140, 66, 0.2);
}
.decision-tree h3 {
font-family: 'Oswald', sans-serif;
color: var(--burnt-orange);
margin-bottom: 25px;
font-size: 2em;
}
.decision-item {
padding: 18px;
margin: 14px 0;
border-left: 5px solid var(--orange-bright);
background: linear-gradient(to right, #FFF3E0 0%, #ffffff 100%);
border-radius: 8px;
}
.decision-item strong {
color: var(--burnt-orange);
font-family: 'Oswald', sans-serif;
}
.footer {
background: linear-gradient(to right, #FFF3E0 0%, #FFF8F0 100%);
padding: 45px;
text-align: center;
color: var(--brown);
border-top: 5px solid var(--orange-main);
}
.footer strong {
font-family: 'Oswald', sans-serif;
color: var(--burnt-orange);
font-size: 1.15em;
}
.lifo-diagram {
background: linear-gradient(135deg, #FFF3E0 0%, #FFF8F0 100%);
border: 4px solid var(--orange-main);
border-radius: 16px;
padding: 30px;
margin: 30px 0;
text-align: center;
}
.lifo-arrow {
color: var(--orange-bright);
font-size: 2.5em;
font-weight: 700;
margin: 10px 0;
}
@media print {
body {
background: white;
padding: 0;
}
.container {
box-shadow: none;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>STACK</h1>
<p>COMPLETE DSA VISUAL GUIDE (C++)</p>
</div>
<!-- SECTION 1: WHAT IS A STACK? -->
<div class="section">
<div class="section-title">
<div class="section-number">01</div>
<span>What is a Stack?</span>
</div>
<p style="font-size: 1.15em;">A <strong>Stack</strong> is a linear data structure that follows the LIFO (Last In First Out) principle. Think of it like a stack of plates - you can only add or remove plates from the top. The last plate you place on the stack is the first one you'll remove.</p>
<h3>Visual Representation - Stack of Elements:</h3>
<div class="stack-visual">
<div class="stack-item">
<div class="stack-value">10</div>
<div class="stack-label">First In, Last Out</div>
</div>
<div class="stack-item">
<div class="stack-value">20</div>
</div>
<div class="stack-item">
<div class="stack-value">30</div>
</div>
<div class="stack-item">
<div class="stack-value">40</div>
<div class="stack-label">Last In, First Out</div>
</div>
</div>
<div class="lifo-diagram">
<h3 style="color: var(--burnt-orange); margin-bottom: 20px;">LIFO Principle</h3>
<div style="font-size: 1.3em; color: var(--brown);">
<div style="margin: 15px 0;">
<strong>PUSH (Insert):</strong> Add to TOP
<div class="lifo-arrow">↓</div>
</div>
<div style="margin: 15px 0;">
<strong>POP (Remove):</strong> Remove from TOP
<div class="lifo-arrow">↑</div>
</div>
</div>
</div>
<div class="code-snippet"><span class="code-comment">// Array-based stack</span>
<span class="code-keyword">int</span> stack[<span class="code-number">100</span>];
<span class="code-keyword">int</span> top = -<span class="code-number">1</span>;
<span class="code-comment">// C++ STL stack</span>
<span class="code-keyword">#include</span> <span class="code-string"><stack></span>
<span class="code-type">std::stack</span><<span class="code-keyword">int</span>> st;
st.<span class="code-type">push</span>(<span class="code-number">10</span>); <span class="code-comment">// Add to top</span>
st.<span class="code-type">pop</span>(); <span class="code-comment">// Remove from top</span>
<span class="code-keyword">int</span> top = st.<span class="code-type">top</span>(); <span class="code-comment">// Access top element</span></div>
<h3>Key Characteristics:</h3>
<ul style="list-style-position: inside; margin-left: 20px; color: var(--brown); line-height: 2.3;">
<li><strong>LIFO Structure</strong> - Last In First Out order</li>
<li><strong>One-end access</strong> - Operations only at top</li>
<li><strong>O(1) operations</strong> - Push, pop, and peek all constant time</li>
<li><strong>No random access</strong> - Can only access top element</li>
<li><strong>Dynamic or fixed size</strong> - Depends on implementation</li>
<li><strong>Essential for recursion</strong> - Function call management</li>
</ul>
</div>
<!-- SECTION 2: MEMORY REPRESENTATION -->
<div class="section">
<div class="section-title">
<div class="section-number">02</div>
<span>Memory Representation</span>
</div>
<h3>Array-Based Stack Memory:</h3>
<div style="background: linear-gradient(135deg, #FFF3E0 0%, #FFF8F0 100%); padding: 35px; border-radius: 14px; margin: 28px 0; border: 4px solid var(--orange-main);">
<p style="margin-bottom: 22px;"><strong>Stack with elements [10, 20, 30]:</strong></p>
<div style="display: flex; gap: 12px; flex-wrap: wrap; margin: 25px 0;">
<div style="text-align: center; flex: 1; min-width: 90px;">
<div style="background: var(--orange-light); border: 3px solid var(--burnt-orange); padding: 12px; border-radius: 8px; margin-bottom: 10px;">
<strong>[0]</strong>
</div>
<div style="background: linear-gradient(to bottom, var(--orange-light), var(--orange-main)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; font-weight: 900; color: white; font-size: 1.5em;">10</div>
</div>
<div style="text-align: center; flex: 1; min-width: 90px;">
<div style="background: var(--orange-light); border: 3px solid var(--burnt-orange); padding: 12px; border-radius: 8px; margin-bottom: 10px;">
<strong>[1]</strong>
</div>
<div style="background: linear-gradient(to bottom, var(--orange-light), var(--orange-main)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; font-weight: 900; color: white; font-size: 1.5em;">20</div>
</div>
<div style="text-align: center; flex: 1; min-width: 90px;">
<div style="background: linear-gradient(to bottom, var(--golden), var(--amber)); border: 3px solid var(--burnt-orange); padding: 12px; border-radius: 8px; margin-bottom: 10px;">
<strong>[2] ← TOP</strong>
</div>
<div style="background: linear-gradient(to bottom, var(--golden), var(--amber)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; font-weight: 900; color: white; font-size: 1.5em; box-shadow: 0 4px 15px rgba(255,193,7,0.5);">30</div>
</div>
<div style="text-align: center; flex: 1; min-width: 90px;">
<div style="background: #E0E0E0; border: 3px solid #9E9E9E; padding: 12px; border-radius: 8px; margin-bottom: 10px;">
<strong>[3]</strong>
</div>
<div style="background: #F5F5F5; border: 4px dashed #BDBDBD; padding: 20px; border-radius: 10px; color: #9E9E9E; font-size: 1.5em;">Empty</div>
</div>
<div style="text-align: center; flex: 1; min-width: 90px;">
<div style="background: #E0E0E0; border: 3px solid #9E9E9E; padding: 12px; border-radius: 8px; margin-bottom: 10px;">
<strong>[4]</strong>
</div>
<div style="background: #F5F5F5; border: 4px dashed #BDBDBD; padding: 20px; border-radius: 10px; color: #9E9E9E; font-size: 1.5em;">Empty</div>
</div>
</div>
<p style="margin-top: 25px; color: var(--brown); font-size: 1.05em;"><strong>Top = 2</strong> (index of last element)</p>
</div>
<h3>Linked List-Based Stack Memory:</h3>
<div style="background: linear-gradient(135deg, #FFF3E0 0%, #FFF8F0 100%); padding: 35px; border-radius: 14px; margin: 28px 0; border: 4px solid var(--orange-main);">
<p style="margin-bottom: 22px;"><strong>Stack as Linked List:</strong></p>
<div style="display: flex; align-items: center; gap: 20px; flex-wrap: wrap;">
<div style="background: linear-gradient(to bottom, var(--golden), var(--amber)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; text-align: center; box-shadow: 0 4px 15px rgba(255,193,7,0.4);">
<div style="font-weight: 700; color: white; font-size: 1.3em;">TOP → 30</div>
<div style="font-size: 0.9em; color: var(--deep-orange); margin-top: 5px;">next ●</div>
</div>
<div style="color: var(--orange-bright); font-size: 2em; font-weight: 700;">→</div>
<div style="background: linear-gradient(to bottom, var(--orange-light), var(--orange-main)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; text-align: center;">
<div style="font-weight: 700; color: white; font-size: 1.3em;">20</div>
<div style="font-size: 0.9em; color: var(--deep-orange); margin-top: 5px;">next ●</div>
</div>
<div style="color: var(--orange-bright); font-size: 2em; font-weight: 700;">→</div>
<div style="background: linear-gradient(to bottom, var(--orange-light), var(--orange-main)); border: 4px solid var(--burnt-orange); padding: 20px; border-radius: 10px; text-align: center;">
<div style="font-weight: 700; color: white; font-size: 1.3em;">10</div>
<div style="font-size: 0.9em; color: #CC0000; margin-top: 5px;">NULL</div>
</div>
</div>
</div>
<div class="code-snippet"><span class="code-comment">// Array implementation</span>
<span class="code-keyword">class</span> <span class="code-type">Stack</span> {
<span class="code-keyword">int</span> arr[<span class="code-number">100</span>];
<span class="code-keyword">int</span> top;
<span class="code-keyword">public</span>:
<span class="code-type">Stack</span>() : top(-<span class="code-number">1</span>) {}
<span class="code-keyword">void</span> <span class="code-type">push</span>(<span class="code-keyword">int</span> x) {
<span class="code-keyword">if</span> (top >= <span class="code-number">99</span>) <span class="code-keyword">return</span>; <span class="code-comment">// Overflow</span>
arr[++top] = x;
}
<span class="code-keyword">int</span> <span class="code-type">pop</span>() {
<span class="code-keyword">if</span> (top < <span class="code-number">0</span>) <span class="code-keyword">return</span> -<span class="code-number">1</span>; <span class="code-comment">// Underflow</span>
<span class="code-keyword">return</span> arr[top--];
}
};
<span class="code-comment">// Linked List implementation</span>
<span class="code-keyword">struct</span> <span class="code-type">Node</span> {
<span class="code-keyword">int</span> data;
<span class="code-type">Node</span>* next;
};
<span class="code-type">Node</span>* top = <span class="code-keyword">nullptr</span>;</div>
<div class="tip-box">
<strong>💡 QUICK TIP:</strong> Array-based stacks are faster but have fixed size. Linked-list stacks are dynamic but use extra memory for pointers!
</div>
</div>
<!-- SECTION 3: TYPES OF STACKS -->
<div class="section">
<div class="section-title">
<div class="section-number">03</div>
<span>Types of Stacks</span>
</div>
<div class="type-card">
<h3>1. Fixed Size Stack (Array-based)</h3>
<p>Pre-allocated array with maximum capacity.</p>
<div class="code-snippet"><span class="code-keyword">int</span> stack[MAX_SIZE];
<span class="code-keyword">int</span> top = -<span class="code-number">1</span>;</div>
<p style="margin-top: 15px;"><strong>Pros:</strong> Fast access, cache-friendly | <strong>Cons:</strong> Fixed capacity, stack overflow possible</p>
</div>
<div class="type-card">
<h3>2. Dynamic Stack (Linked List)</h3>
<p>Linked list of nodes, grows as needed.</p>
<div class="code-snippet"><span class="code-keyword">struct</span> <span class="code-type">Node</span> {
<span class="code-keyword">int</span> data;
<span class="code-type">Node</span>* next;
};
<span class="code-type">Node</span>* top = <span class="code-keyword">nullptr</span>;</div>
<p style="margin-top: 15px;"><strong>Pros:</strong> No size limit (within memory) | <strong>Cons:</strong> Extra pointer memory, slower</p>
</div>
<div class="type-card">
<h3>3. Dynamic Array Stack (Vector-based)</h3>
<p>Uses dynamic array that automatically resizes.</p>
<div class="code-snippet"><span class="code-type">std::vector</span><<span class="code-keyword">int</span>> stack;</div>
<p style="margin-top: 15px;"><strong>Pros:</strong> Automatic resizing, STL benefits | <strong>Cons:</strong> Occasional reallocation cost</p>
</div>
<div class="type-card">
<h3>4. std::stack (STL Adapter)</h3>
<p>Standard template library stack - most commonly used.</p>
<div class="code-snippet"><span class="code-keyword">#include</span> <span class="code-string"><stack></span>
<span class="code-type">std::stack</span><<span class="code-keyword">int</span>> st;
st.<span class="code-type">push</span>(<span class="code-number">10</span>);
st.<span class="code-type">pop</span>();
<span class="code-keyword">int</span> top = st.<span class="code-type">top</span>();</div>
<p style="margin-top: 15px;"><strong>Pros:</strong> Easy to use, well-tested | <strong>Container:</strong> Uses deque by default</p>
</div>
<div class="type-card">
<h3>5. Min Stack / Max Stack</h3>
<p>Special stack that tracks minimum or maximum element in O(1).</p>
<div class="code-snippet"><span class="code-comment">// Tracks minimum at each level</span>
<span class="code-type">std::stack</span><<span class="code-keyword">int</span>> mainStack;
<span class="code-type">std::stack</span><<span class="code-keyword">int</span>> minStack;</div>
<p style="margin-top: 15px;"><strong>Use Case:</strong> Get min/max in constant time</p>
</div>
<div class="type-card">
<h3>6. Monotonic Stack</h3>
<p>Elements maintained in sorted order (increasing or decreasing).</p>
<div class="code-snippet"><span class="code-comment">// Keep stack in increasing order</span>
<span class="code-keyword">while</span> (!st.<span class="code-type">empty</span>() && st.<span class="code-type">top</span>() > current) {
st.<span class="code-type">pop</span>();
}
st.<span class="code-type">push</span>(current);</div>
<p style="margin-top: 15px;"><strong>Use Case:</strong> Next greater/smaller element problems</p>
</div>
</div>
<!-- Continue in next part -->
<!-- SECTION 4: WHY STACKS? -->
<div class="section">
<div class="section-title">
<div class="section-number">04</div>
<span>Why Stacks? (Comparison)</span>
</div>
<h3>Stack vs Array:</h3>
<table class="comparison-table">
<thead>
<tr>
<th>Feature</th>
<th>Stack</th>
<th>Array</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Access</strong></td>
<td>Top only - O(1)</td>
<td>Any index - O(1)</td>
</tr>
<tr>
<td><strong>Insert</strong></td>
<td>Push at top - O(1)</td>
<td>End O(1), Elsewhere O(n)</td>
</tr>
<tr>
<td><strong>Delete</strong></td>
<td>Pop from top - O(1)</td>
<td>End O(1), Elsewhere O(n)</td>
</tr>
<tr>
<td><strong>Order</strong></td>
<td>LIFO (Last In First Out)</td>
<td>Sequential access</td>
</tr>
<tr>
<td><strong>Use Case</strong></td>
<td>Function calls, undo/redo</td>
<td>General data storage</td>
</tr>
<tr>
<td><strong>Random Access</strong></td>
<td>❌ No</td>
<td>✅ Yes</td>
</tr>
</tbody>
</table>
<h3 style="margin-top: 35px;">Stack vs Queue:</h3>
<table class="comparison-table">
<thead>
<tr>
<th>Feature</th>
<th>Stack</th>
<th>Queue</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Order</strong></td>
<td>LIFO (Last In First Out)</td>
<td>FIFO (First In First Out)</td>
</tr>
<tr>
<td><strong>Operations</strong></td>
<td>Push/Pop from top</td>
<td>Enqueue rear, Dequeue front</td>
</tr>
<tr>
<td><strong>Access Points</strong></td>
<td>One end (top)</td>
<td>Two ends (front & rear)</td>
</tr>
<tr>
<td><strong>Use Case</strong></td>
<td>Function calls, backtracking</td>
<td>Job scheduling, BFS</td>
</tr>
</tbody>
</table>
<h3 style="margin-top: 35px;">When to Use Stack:</h3>
<div class="type-card">
<ul style="list-style-position: inside; line-height: 2.2;">
<li>✅ Need LIFO (Last In First Out) behavior</li>
<li>✅ Function call management / recursion</li>
<li>✅ Undo/Redo operations in applications</li>
<li>✅ Expression evaluation (infix, postfix, prefix)</li>
<li>✅ Backtracking algorithms (maze, N-Queens)</li>
<li>✅ Balanced parentheses checking</li>
<li>✅ Browser history (back button)</li>
<li>✅ Depth-First Search (DFS) traversal</li>
</ul>
</div>
</div>
<!-- SECTION 5: BASIC OPERATIONS -->
<div class="section">
<div class="section-title">
<div class="section-number">05</div>
<span>Basic Operations</span>
</div>
<div class="operation-grid">
<div class="operation-card">
<h4>📥 Push (Insert)</h4>
<p style="margin-bottom: 15px;">Add element to top of stack</p>
<ul>
<li>Time: O(1)</li>
<li>Check for overflow first</li>
<li>Increment top pointer</li>
<li>Add element at top</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">void</span> <span class="code-type">push</span>(<span class="code-keyword">int</span> x) {
<span class="code-keyword">if</span> (top >= MAX-<span class="code-number">1</span>)
<span class="code-keyword">return</span>; <span class="code-comment">// Overflow</span>
arr[++top] = x;
}</div>
</div>
<div class="operation-card">
<h4>📤 Pop (Remove)</h4>
<p style="margin-bottom: 15px;">Remove and return top element</p>
<ul>
<li>Time: O(1)</li>
<li>Check for underflow first</li>
<li>Return top element</li>
<li>Decrement top pointer</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">int</span> <span class="code-type">pop</span>() {
<span class="code-keyword">if</span> (top < <span class="code-number">0</span>)
<span class="code-keyword">return</span> -<span class="code-number">1</span>; <span class="code-comment">// Underflow</span>
<span class="code-keyword">return</span> arr[top--];
}</div>
</div>
<div class="operation-card">
<h4>👀 Top / Peek</h4>
<p style="margin-bottom: 15px;">View top element without removing</p>
<ul>
<li>Time: O(1)</li>
<li>Check if stack empty</li>
<li>Return top element</li>
<li>Don't modify stack</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">int</span> <span class="code-type">peek</span>() {
<span class="code-keyword">if</span> (top < <span class="code-number">0</span>)
<span class="code-keyword">return</span> -<span class="code-number">1</span>;
<span class="code-keyword">return</span> arr[top];
}</div>
</div>
<div class="operation-card">
<h4>❓ isEmpty</h4>
<p style="margin-bottom: 15px;">Check if stack has no elements</p>
<ul>
<li>Time: O(1)</li>
<li>Compare top with -1</li>
<li>Return true if empty</li>
<li>Essential before pop/peek</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">bool</span> <span class="code-type">isEmpty</span>() {
<span class="code-keyword">return</span> top < <span class="code-number">0</span>;
}</div>
</div>
<div class="operation-card">
<h4>⚠️ isFull</h4>
<p style="margin-bottom: 15px;">Check if stack reached capacity</p>
<ul>
<li>Time: O(1)</li>
<li>Only for array-based</li>
<li>Compare top with MAX-1</li>
<li>Prevents overflow</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">bool</span> <span class="code-type">isFull</span>() {
<span class="code-keyword">return</span> top >= MAX-<span class="code-number">1</span>;
}</div>
</div>
<div class="operation-card">
<h4>📏 Size</h4>
<p style="margin-bottom: 15px;">Get number of elements in stack</p>
<ul>
<li>Time: O(1)</li>
<li>Return top + 1</li>
<li>0 if stack empty</li>
<li>Useful for iteration</li>
</ul>
<div class="code-snippet" style="margin-top: 15px; font-size: 0.85em;"><span class="code-keyword">int</span> <span class="code-type">size</span>() {
<span class="code-keyword">return</span> top + <span class="code-number">1</span>;
}</div>
</div>
</div>
<div class="tip-box">
<strong>💡 REMEMBER:</strong> Always check isEmpty() before pop() or top() to avoid underflow errors!
</div>
</div>
<!-- SECTION 6: COMMON MISTAKES -->
<div class="section">
<div class="section-title">
<div class="section-number">06</div>
<span>Common Mistakes & Pitfalls</span>
</div>
<div class="mistake-card">
<h4>❌ Stack Overflow (Push when full)</h4>
<p>Most common error - pushing to a full stack causes overflow.</p>
<div class="code-snippet"><span class="code-comment">// ❌ WRONG - No overflow check</span>