stack

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 2 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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