Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stack ¶
type Stack[V any] []V // Stack is a stack of any type
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) for v := range s.All() { fmt.Println(v) } }
Output: 5 4 3 2 1
func NewStack ¶
NewStack creates a new stack with the given values if any If no values are given, an empty stack stack is created, however, the compiler will require the type to be specified.
func (*Stack[V]) All ¶
All returns a iter.Seq[V] of all the elements in the stack. It returns a sigle use iterator.
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) s.All()(func(v int) bool { fmt.Println(v) return true }) fmt.Println(s.IsEmpty()) }
Output: 5 4 3 2 1 true
func (*Stack[V]) Enumerate ¶
Enumerate returns an iter.Seq2[int, V] of all the elements in the stack and their index. The iteration empties the stack It returns a sigle use iterator.
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) s.Enumerate(0)(func(i int, v int) bool { fmt.Println(i, v) return true }) fmt.Println(s.IsEmpty()) }
Output: 0 5 1 4 2 3 3 2 4 1 true
func (Stack[V]) IsEmpty ¶
IsEmpty returns true if the stack is empty
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack[int]() fmt.Println(s.IsEmpty()) s.Push(1) fmt.Println(s.IsEmpty()) }
Output: true false
func (Stack[V]) Len ¶
Len returns the number of elements in the stack
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) fmt.Println(s.Len()) }
Output: 5
func (Stack[V]) Peek ¶
func (s Stack[V]) Peek() V
Peek returns the top value of the stack without removing it
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) fmt.Println(s.Peek()) s.Pop() fmt.Println(s.Peek()) }
Output: 5 4
func (*Stack[V]) Pop ¶
func (s *Stack[V]) Pop() V
Pop removes and returns the top value of the stack
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3, 4, 5) fmt.Println(s.Pop()) fmt.Println(s.Pop()) fmt.Println(s.Pop()) }
Output: 5 4 3
func (*Stack[V]) Push ¶
func (s *Stack[V]) Push(val ...V)
Push adds the given values to the stack
Example ¶
package main import ( "fmt" stk "github.com/elordeiro/go/container/stack" ) func main() { s := stk.NewStack(1, 2, 3) s.Push(4, 5, 6) for v := range s.All() { fmt.Println(v) } }
Output: 6 5 4 3 2 1