Documentation
¶
Overview ¶
Package stack provides a stack data structure implementation.
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 an implementation of a stack LIFO data structure.
func New ¶
New creates a new stack with the given values if any are provided.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
for v := range s.All() {
fmt.Println(v)
}
}
Output: 5 4 3 2 1
func (Stack[V]) All ¶
All returns a iter.Seq[V] of all the elements in the stack.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
for v := range s.All() {
fmt.Println(v)
}
}
Output: 5 4 3 2 1
func (Stack[V]) Backwards ¶
Backwards returns a iter.Seq[V] of all the elements in the stack in reverse order.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
for v := range s.Backwards() {
fmt.Println(v)
}
}
Output: 1 2 3 4 5
func (*Stack[V]) Drain ¶
Drain returns a iter.Seq[V] of all the elements in the stack by popping them. The stack will be empty after this operation. It returns a single use iterator.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
for v := range s.Drain() {
fmt.Println(v)
}
fmt.Println(s.IsEmpty())
}
Output: 5 4 3 2 1 true
func (Stack[V]) IsEmpty ¶
IsEmpty returns true if the stack is empty.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New[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"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
fmt.Println(s.Len())
}
Output: 5
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"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(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"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3)
s.Push(4, 5, 6)
for v := range s.All() {
fmt.Println(v)
}
}
Output: 6 5 4 3 2 1
func (Stack[V]) String ¶
String returns a string representation of the stack.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
fmt.Println(s.String())
}
Output: $[5 4 3 2 1]
func (Stack[V]) Top ¶
func (s Stack[V]) Top() V
Top returns the top value of the stack without removing it.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/stack"
)
func main() {
s := stack.New(1, 2, 3, 4, 5)
fmt.Println(s.Top())
s.Pop()
fmt.Println(s.Top())
}
Output: 5 4