stack

package
v0.1.4 Latest Latest
Warning

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

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

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

func New[V any](vals ...V) *Stack[V]

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

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

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

func (s Stack[V]) Backwards() iter.Seq[V]

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

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

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

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

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

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

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

func (s Stack[V]) String() 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

Jump to

Keyboard shortcuts

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