stack

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

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

func NewStack[V any](vals ...V) Stack[V]

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

func (s *Stack[V]) All() iter.Seq[V]

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

func (s *Stack[V]) Enumerate(start int) iter.Seq2[int, V]

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

func (s Stack[V]) IsEmpty() bool

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

func (s Stack[V]) Len() int

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

func (Stack[V]) String

func (s Stack[V]) String() string

String returns a string representation of the stack

Jump to

Keyboard shortcuts

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