stack

package
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 1 Imported by: 0

README

Stack

This package provides a generic stack data structure for Go, supporting any type.

Features

  • Generic: Works with any type (Stack[T any]).
  • Push: Add an element to the top of the stack.
  • Pop: Remove and return the top element (returns zero value and false if empty).
  • Peek: View the top element without removing it.
  • PeekNthElement: View the n-th element from the top without removing it.
  • IsEmpty: Check if the stack is empty.
  • Size: Get the number of elements in the stack.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a generic thread-safe stack data structure.

func New

func New[T any]() *Stack[T]

New creates a new Stack.

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty returns true if the stack is empty.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

Peek returns the top element without removing it. Returns the zero value and false if the stack is empty.

func (*Stack[T]) PeekNthElement added in v1.17.0

func (s *Stack[T]) PeekNthElement(n int) (T, bool)

PeekNthElement returns the element at position n from the top without removing it. n=0 returns the top element, n=1 returns the second element from top, etc. Returns the zero value and false if the index is out of bounds.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

Pop removes and returns the top element of the stack. Returns the zero value and false if the stack is empty.

func (*Stack[T]) Push

func (s *Stack[T]) Push(value T)

Push adds an element to the top of the stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the number of elements in the stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL