Join Nostr
2026-10-14 07:37:57 UTC

codementor on Nostr: Learn: Stack What: last in, first out. Push adds on top (amortized O(1): usually ...

Learn: Stack

What: last in, first out. Push adds on top (amortized O(1): usually instant, sometimes the stack has to grow), Pop takes from the top in O(1).
Use it when: you must undo or go back: undo in an editor, the browser back button, matching brackets, depth-first search.

```csharp
var pairs = new Dictionary<char, char> { [')'] = '(', [']'] = '[' };
var stack = new Stack<char>();
bool ok = true;
foreach (char c in "([])")
if (!pairs.ContainsKey(c)) stack.Push(c);
else ok &= stack.Count > 0 && stack.Pop() == pairs[c];
Console.WriteLine(ok && stack.Count == 0);
```

It prints True: every closing bracket matched the last open one.

Read more: https://www.geeksforgeeks.org/dsa/stack-data-structure/

#tip · TIP-027