Stacks
May 31, 2026 ยท View on GitHub
Stacks
This document covers the stack implementation in containers.
Overview
The stack provides a last-in, first-out collection backed by a linked structure.
It is useful when you need:
- push/pop behavior
- a simple LIFO collection
- a compact way to manage ordered temporary state
Common operations
- create a new stack
- push values
- pop the top value
- peek at the top value
- inspect the stack size
- retrieve all values
API reference
Stack
| Method | Purpose |
|---|---|
New | Creates a new stack |
Push | Pushes a value onto the stack |
Pop | Removes and returns the top value |
Peek | Returns the top value without removing it |
IsEmpty | Reports whether the stack is empty |
Size | Returns the number of elements |
Clear | Removes all elements from the stack |
Values | Returns all values as a slice |
Notes
- A stack is a natural fit for LIFO workflows.
- Empty stack operations should be handled carefully by callers.
When to use it
Use the stack when:
- the most recently added item should be processed first
- the code naturally follows a LIFO pattern
- you want a simple abstraction over a linked collection