Documentation
¶
Index ¶
Examples ¶
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 }
A stack is a first-in-last-out (FILO) data structure: the last element pushed onto the stack is the first one popped from it. The zero value is an empty stack and ready to use. A stack is safe for concurrent use.
func (*Stack[T]) CheckPop ¶
CheckPop returns the value at the top of the stack and a boolean indicating whether the stack is empty. If the stack is empty, this returns the zero value of the stack's type and false.
Example ¶
package main import ( "fmt" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[uint8] top1, ok1 := s.CheckPop() s.Push(3) s.Push(5) top2, ok2 := s.CheckPop() top3, ok3 := s.CheckPop() top4, ok4 := s.CheckPop() fmt.Println(top1, ok1) fmt.Println(top2, ok2) fmt.Println(top3, ok3) fmt.Println(top4, ok4) }
Output: 0 false 5 true 3 true 0 false
func (*Stack[T]) Clear ¶
func (s *Stack[T]) Clear()
Clear removes all elements from the stack.
Example ¶
package main import ( "fmt" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[bool] s.Push(true) s.Push(false) count1 := s.Count() s.Clear() count2 := s.Count() fmt.Println(count1, count2) }
Output: 2 0
func (*Stack[T]) Count ¶
Count returns the number of elements in the stack.
Example ¶
package main import ( "fmt" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[byte] fmt.Println(s.Count()) for _, b := range []byte{'a', 'b', 'c'} { s.Push(b) fmt.Println(s.Count()) } s.Pop() fmt.Println(s.Count()) s.Peek() fmt.Println(s.Count()) }
Output: 0 1 2 3 2 2
func (*Stack[T]) Empty ¶
Empty returns true if the stack is empty.
Example ¶
package main import ( "fmt" "math" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[float64] isEmpty1 := s.Empty() s.Push(math.Pi) isEmpty2 := s.Empty() s.Pop() isEmpty3 := s.Empty() fmt.Println(isEmpty1, isEmpty2, isEmpty3) }
Output: true false true
func (*Stack[T]) Peek ¶
func (s *Stack[T]) Peek() (t T)
Peek returns the value at the top of the stack without removing it. If the stack is empty, this returns the zero value of the stack's type.
Example ¶
package main import ( "fmt" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[[]string] top1 := s.Peek() s.Push([]string{"a", "b", "c"}) s.Push([]string{"d", "e", "f"}) top2 := s.Peek() top3 := s.Peek() fmt.Println(top1) fmt.Println(top2) fmt.Println(top3) }
Output: [] [d e f] [d e f]
func (*Stack[T]) Pop ¶
func (s *Stack[T]) Pop() (t T)
Pop removes and returns the value at the top of the stack. If the stack is empty, this returns the zero value of the stack's type.
Example ¶
package main import ( "fmt" "github.com/green-aloe/utilities/stack" ) func main() { var s stack.Stack[string] top1 := s.Pop() s.Push("hello") s.Push("world") top2 := s.Pop() top3 := s.Pop() top4 := s.Pop() fmt.Println(top1) fmt.Println(top2) fmt.Println(top3) fmt.Println(top4) }
Output: world hello