stack

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT 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[T any] interface {
	// Len returns size of Stack
	Len() int
	// Push pushes element to head of Stack
	Push(v T)
	// Pop pops element from head of Stack
	Pop() (T, bool)
	// Top returns top element of Stack
	Top() (T, bool)
	// Clear releases all the entries, resulting in an empty Stack.
	Clear()
	// Range calls f sequentially for each element present in the Stack.
	// If f returns false, range stops the iteration.
	Range(func(T) bool)
}
Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/container/stack"
)

func main() {
	for _, s := range []stack.Stack[any]{
		stack.NewSafeStack[any](),
		stack.NewStack[any](),
	} {
		s.Push(1)
		s.Push(2)
		s.Push(3)
		s.Push(4)

		top, _ := s.Top()
		fmt.Println("top:", top)

		v, _ := s.Pop()
		fmt.Println(v)

		for v = range s.Range {
			fmt.Println(v)
		}
		s.Clear()
		fmt.Println("len:", s.Len())
		_, ok := s.Top()
		fmt.Println("top:", ok)
		_, ok = s.Pop()
		fmt.Println("pop:", ok)
	}

}
Output:
top: 4
4
3
2
1
len: 0
top: false
pop: false
top: 4
4
3
2
1
len: 0
top: false
pop: false

func NewSafeStack

func NewSafeStack[T any]() Stack[T]

func NewStack

func NewStack[T any]() Stack[T]

Jump to

Keyboard shortcuts

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