stack

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 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() int
	Push(v T)
	Pop() (T, bool)
	Top() (T, bool)
	Clear()
}
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)
		for {
			if v, ok := s.Pop(); ok {
				fmt.Println(v)
				continue
			}
			break
		}
		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