Documentation | Contributing | Code of Conduct
import "atomicgo.dev/stack"Package stack is a simple implemntation of a stack data structure. It uses generics to make it type safe.
- type Stack
- func New[T any]() Stack[T]
- func (s *Stack[T]) Clear()
- func (s *Stack[T]) Contains(item T) bool
- func (s *Stack[T]) IsEmpty() bool
- func (s *Stack[T]) Peek() T
- func (s *Stack[T]) Pop() T
- func (s *Stack[T]) PopSafe() T
- func (s *Stack[T]) Push(item ...T)
- func (s *Stack[T]) Size() int
- func (s Stack[T]) String() string
- func (s *Stack[T]) Values() []T
type Stack
Stack is a simple implementation of a stack data structure.
type Stack[T any] struct { // contains filtered or unexported fields }func New
func New[T any]() Stack[T]New returns a new stack.
package main import ( "atomicgo.dev/stack" ) func main() { stack.New[string]() }func (*Stack[T]) Clear
func (s *Stack[T]) Clear()Clear removes all items from the stack.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") s.Clear() fmt.Println(s) }[] func (*Stack[T]) Contains
func (s *Stack[T]) Contains(item T) boolContains returns true if the stack contains the item.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.Contains("Hello")) fmt.Println(s.Contains("Foo")) }true false func (*Stack[T]) IsEmpty
func (s *Stack[T]) IsEmpty() boolIsEmpty returns true if the stack is empty.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.IsEmpty()) s.Clear() fmt.Println(s.IsEmpty()) }false true func (*Stack[T]) Peek
func (s *Stack[T]) Peek() TPeek returns the top item of the stack without removing it.
func (*Stack[T]) Pop
func (s *Stack[T]) Pop() TPop removes an item from the stack and returns it. Panics if the stack is empty. Use PopSafe for safer access to the Stack.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.Pop()) fmt.Println(s.Pop()) }World Hello func (*Stack[T]) PopSafe
func (s *Stack[T]) PopSafe() TPopSafe removes an item from the stack and returns it. Returns the zero value of the type if the stack is empty. To make this function safe, it uses reflection and is therefore slower than Pop.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.PopSafe()) fmt.Println(s.PopSafe()) fmt.Println(s.PopSafe()) }World Hello func (*Stack[T]) Push
func (s *Stack[T]) Push(item ...T)Push adds items to a stack.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s) }[Hello World] func (*Stack[T]) Size
func (s *Stack[T]) Size() intSize returns the size of the stack.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.Size()) }2 func (Stack[T]) String
func (s Stack[T]) String() stringpackage main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.String()) }[Hello World] func (*Stack[T]) Values
func (s *Stack[T]) Values() []TValues returns the values of the stack as a slice.
package main import ( "fmt" "atomicgo.dev/stack" ) func main() { s := stack.New[string]() s.Push("Hello") s.Push("World") fmt.Println(s.Values()) }[Hello World] Generated by gomarkdoc
AtomicGo.dev Β Β·Β with β€οΈ by @MarvinJWendt | MarvinJWendt.com
