-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_Stack.cs
More file actions
136 lines (117 loc) · 6.14 KB
/
Copy path07_Stack.cs
File metadata and controls
136 lines (117 loc) · 6.14 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
using System;
using System.Collections.Generic;
namespace DataStructures
{
/// <summary>
/// Demonstrates the Stack<T> collection in C# (LIFO — Last In, First Out).
/// Elements are pushed onto the top and popped from the top.
///
/// Common use cases: undo/redo operations, expression evaluation,
/// depth-first search (DFS), browser history, bracket matching,
/// digit decomposition, call stacks.
/// </summary>
public class StackExamples
{
// ─── Entry Point ───────────────────────────────────────────────
public static void Main()
{
Console.WriteLine("═══════════════════════════════════════════════");
Console.WriteLine(" C# Stack<T> Examples ");
Console.WriteLine("═══════════════════════════════════════════════\n");
// ── 1. Basic Push / Pop / Peek ──────────────────────────────
Console.WriteLine("── Basic Push / Pop / Peek ────────────────────");
var stack = new Stack<string>();
// Push — add elements to the top
stack.Push("First");
stack.Push("Second");
stack.Push("Third");
stack.Push("Fourth");
Console.WriteLine($" Stack (top→bottom): {string.Join(", ", stack)}");
Console.WriteLine($" Count: {stack.Count}");
Console.WriteLine();
// Peek — view the top element without removing it
Console.WriteLine($" Peek() → \"{stack.Peek()}\" (stack unchanged)");
Console.WriteLine($" Count after Peek: {stack.Count}");
Console.WriteLine();
// Pop — remove and return the top element (LIFO)
Console.WriteLine(" Popping all elements:");
while (stack.Count > 0)
{
string item = stack.Pop();
Console.WriteLine($" Popped: \"{item}\" | Remaining: {stack.Count}");
}
Console.WriteLine();
// ── 2. Number Digit Decomposition ──────────────────────────
Console.WriteLine("── Digit Decomposition using Stack ───────────");
int number = 12345;
Console.WriteLine($" Original number: {number}");
var digitStack = new Stack<int>();
int temp = number;
// Push each digit onto the stack
while (temp > 0)
{
digitStack.Push(temp % 10); // Extract last digit
temp /= 10; // Remove last digit
}
Console.WriteLine($" Digits (stack order): {string.Join(", ", digitStack)}");
// Pop digits — reveals them in reverse order
Console.WriteLine(" Popping digits (reversed):");
while (digitStack.Count > 0)
{
Console.WriteLine($" Digit: {digitStack.Pop()}");
}
Console.WriteLine();
// Reverse the number using stack
temp = number;
var reverseStack = new Stack<int>();
while (temp > 0)
{
reverseStack.Push(temp % 10);
temp /= 10;
}
// Build reversed number
int reversed = 0;
int multiplier = 1;
while (reverseStack.Count > 0)
{
reversed += reverseStack.Pop() * multiplier;
multiplier *= 10;
}
Console.WriteLine($" Reversed number: {reversed}");
Console.WriteLine();
// ── 3. ASCII Character Stack ────────────────────────────────
Console.WriteLine("── ASCII Character Stack ─────────────────────");
var charStack = new Stack<char>();
// Push ASCII characters A–E
for (char c = 'A'; c <= 'E'; c++)
{
charStack.Push(c);
Console.WriteLine($" Pushed: '{c}' (ASCII {(int)c})");
}
Console.WriteLine($" Stack: [{string.Join(", ", charStack)}]");
// Pop and show characters in reverse
Console.WriteLine(" Popping (LIFO — reverse of push order):");
while (charStack.Count > 0)
{
char ch = charStack.Pop();
Console.WriteLine($" '{ch}' — ASCII {(int)ch}");
}
Console.WriteLine();
// ── 4. ToArray & Contains ──────────────────────────────────
Console.WriteLine("── ToArray / Contains ────────────────────────");
var nums = new Stack<int>();
nums.Push(10);
nums.Push(20);
nums.Push(30);
int[] arr = nums.ToArray();
Console.WriteLine($" Stack: {string.Join(", ", nums)}");
Console.WriteLine($" Array: {string.Join(", ", arr)}");
Console.WriteLine($" Contains(20): {nums.Contains(20)}");
Console.WriteLine($" Contains(99): {nums.Contains(99)}");
Console.WriteLine("\n═══════════════════════════════════════════════");
Console.WriteLine(" Complexity: Push O(1) | Pop O(1) | Peek O(1)");
Console.WriteLine(" Contains O(n) | ToArray O(n)");
Console.WriteLine("═══════════════════════════════════════════════");
}
}
}