stack

package
v0.0.0-...-5bd4ecb Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package stack implements a generic stack data structure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

type Stack[T any] []T

Stack represents the stack data structure.

func New

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

New returns an initialized stack, optionally with the given elements. The elements are pushed in the same order as provided.

func (*Stack[T]) IsEmpty

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

IsEmpty is used to check whether the stack is empty or not.

func (*Stack[T]) Len

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

Len returns the number of elements on the stack.

func (*Stack[T]) Peek

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

Peek returns the top element on the stack without removing it, or nil if the stack is empty.

An attempt to peek when the stack is empty will return the zero value for the type of the elements in the stack. Using multiple assignment, one can distinguish a missing entry from a zero value. This is referred to as the "comma ok" idiom.

func (*Stack[T]) Pop

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

Pop removes the top element on the stack and returns it, or nil if the stack is empty.

An attempt to pop when the stack is empty will return the zero value for the type of the elements in the stack. Using multiple assignment, one can distinguish a missing entry from a zero value. This is referred to as the "comma ok" idiom.

func (*Stack[T]) Push

func (s *Stack[T]) Push(e ...T)

Push adds an element to the top of a stack. Multiple elements are added in the same order as provided.

func (*Stack[T]) String

func (s *Stack[T]) String() string

Jump to

Keyboard shortcuts

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