Stacks

May 31, 2026 ยท View on GitHub

sink-logo

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

MethodPurpose
NewCreates a new stack
PushPushes a value onto the stack
PopRemoves and returns the top value
PeekReturns the top value without removing it
IsEmptyReports whether the stack is empty
SizeReturns the number of elements
ClearRemoves all elements from the stack
ValuesReturns 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

Examples