stack

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 4 Imported by: 1

README

Go Stack

中文文档

English Document

一、这是什么?为什么选它?优势是啥?

这个项目是各种类型的栈的Go语言实现版本。

  • 支持泛型,API使用更方便,避免类型强转
  • 对每种类型的栈都提供了线程安全版本,使用的时候可以专注业务不需要再考虑锁的问题了
  • 支持的栈类型更丰富,如果有更多有意思的栈,请提issues call me实现

二、安装

go get github.com/golang-infrastructure/go-stack

三、目前实现的栈

  • ArrayStack[T any]
  • SyncArrayStack[T any]
  • LinkedStack[T any]
  • SyncLinkedStack[T any]
  • MinStack[T any]
  • SyncMinStack[T any]
  • MaxStack[T any]
  • SyncMaxStack [T any]
一览表
Struct名称 线程安全性 阻塞性 特有特性
ArrayStack[T any] × ×
SyncArrayStack[T any] ×
LinkedStack[T any] × ×
SyncLinkedStack[T any] ×
MinStack[T any] × × O(1)获取栈中最小值
SyncMinStack[T any] × O(1)获取栈中最小值
MaxStack[T any] × × O(1)获取栈中最大值
SyncMaxStack [T any] × O(1)获取栈中最大值

四、Interface: Stack

接口Stack[T any]定义了一些所有的栈都会有的API。

入栈

Push(values ...T)

Example:

stack := NewArrayStack[int]()
stack.Push(1)

出栈

Pop() T
PopE() (T, error)

Pop Example:

type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
// Output:
// <nil>
// &{}

PopE Example:

stack := NewArrayStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
    fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
    fmt.Println(err.Error())
    return
}
fmt.Println(element)
// Output:
// stack empty!
// 1

查看栈顶元素

Peek() T
PeekE() (T, error)

Peek Example:

type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
// Output:
// <nil>
// &{}

PeekE Example:

stack := NewArrayStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
    fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
    fmt.Println(err.Error())
    return
}
fmt.Println(element)
// Output:
// stack empty!
// 1

判断栈是否空

IsEmpty() bool
IsNotEmpty() bool

IsEmpty Example:

stack := NewArrayStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
// Output:
// true
// false

IsNotEmpty Example:

stack := NewArrayStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
// Output:
// false
// true

栈中元素个数

Size() int

Example:

stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
// Output:
// 1

清空栈

Clear() 

Example:

stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
// Output:
// 1
// 0

String

把栈转为String,一般用于debug之类的把栈中所有元素可视化。

Example:

stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.String())
// Output:
// [1]

ArrayStack

基于数组实现的栈。

Example:

stack := NewArrayStack[int]()
fmt.Println(stack.String())
// Output:
// []

LinkedStack

基于链表实现的栈。

Example:

stack := NewLinkedStack[int]()
fmt.Println(stack.String())
// Output:
// []

五、最大栈 & 最小栈

MinStack & SyncMinStack

相较于Stack接口增加了两个方法用于O(1)获取栈中所有元素的最小值:

GetMin() T
GetMinE() (T, error)

GetMin Example:

stack := NewSyncMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
assert.ErrorIs(t, err, ErrStackEmpty)

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
assert.Nil(t, err)
assert.Equal(t, 7, element)

GetMinE Example:

stack := NewSyncMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
    fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
    fmt.Println(err.Error())
    return
}
fmt.Println(element)
// Output:
// stack empty!
// 7

MaxStack & SyncMaxStack

相较于Stack接口增加了两个方法用于O(1)获取栈中所有元素的最小值:

GetMax() T 
GetMaxE() (T, error) 

GetMax Example:

stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
assert.ErrorIs(t, err, ErrStackEmpty)

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
assert.Nil(t, err)
assert.Equal(t, 10, element)

GetMaxE Example:

stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
if errors.Is(err, ErrStackEmpty) {
    fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
if err != nil {
    fmt.Println(err.Error())
    return
}
fmt.Println(element)
// Output:
// stack empty!
// 10

TODO

  • 为Stack实现JSON序列化接口
  • 实现阻塞栈,因为仅当多个协程操作同一个栈时才需要考虑阻塞的情况,所以阻塞栈都是线程安全的。

Documentation

Index

Examples

Constants

View Source
const EmptyStackTopIndex = -1

EmptyStackTopIndex 当栈为空的时候栈顶的指针指向的下标位置

Variables

View Source
var ErrStackEmpty = errors.New("stack empty")

Functions

This section is empty.

Types

type ArrayStack

type ArrayStack[T any] struct {
	// contains filtered or unexported fields
}

ArrayStack 数组实现的栈

func NewArrayStack

func NewArrayStack[T any]() *ArrayStack[T]
Example
stack := NewArrayStack[int]()
fmt.Println(stack.String())
Output:

[]

func (*ArrayStack[T]) Clear

func (x *ArrayStack[T]) Clear()
Example
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*ArrayStack[T]) IsEmpty

func (x *ArrayStack[T]) IsEmpty() bool
Example
stack := NewArrayStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*ArrayStack[T]) IsNotEmpty

func (x *ArrayStack[T]) IsNotEmpty() bool
Example
stack := NewArrayStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*ArrayStack[T]) Peek

func (x *ArrayStack[T]) Peek() T
Example
type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*ArrayStack[T]) PeekE

func (x *ArrayStack[T]) PeekE() (T, error)
Example
stack := NewArrayStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*ArrayStack[T]) Pop

func (x *ArrayStack[T]) Pop() T
Example
type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*ArrayStack[T]) PopE

func (x *ArrayStack[T]) PopE() (T, error)
Example
stack := NewArrayStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*ArrayStack[T]) Push

func (x *ArrayStack[T]) Push(values ...T)
Example
stack := NewArrayStack[int]()
stack.Push(1)

func (*ArrayStack[T]) Size

func (x *ArrayStack[T]) Size() int
Example
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*ArrayStack[T]) String

func (x *ArrayStack[T]) String() string
Example
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.String())
Output:

[1]

type Comparator

type Comparator[T any] func(a T, b T) int

type LinkedStack

type LinkedStack[T any] struct {
	// contains filtered or unexported fields
}

LinkedStack 基于链表实现的栈

func NewLinkedStack

func NewLinkedStack[T any]() *LinkedStack[T]
Example
stack := NewLinkedStack[int]()
fmt.Println(stack.String())
Output:

[]

func (*LinkedStack[T]) Clear

func (x *LinkedStack[T]) Clear()
Example
stack := NewLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*LinkedStack[T]) IsEmpty

func (x *LinkedStack[T]) IsEmpty() bool
Example
stack := NewLinkedStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*LinkedStack[T]) IsNotEmpty

func (x *LinkedStack[T]) IsNotEmpty() bool
Example
stack := NewLinkedStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*LinkedStack[T]) Peek

func (x *LinkedStack[T]) Peek() T
Example
type User struct {
}
stack := NewLinkedStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*LinkedStack[T]) PeekE

func (x *LinkedStack[T]) PeekE() (T, error)
Example
stack := NewLinkedStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*LinkedStack[T]) Pop

func (x *LinkedStack[T]) Pop() T
Example
type User struct {
}
stack := NewLinkedStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*LinkedStack[T]) PopE

func (x *LinkedStack[T]) PopE() (T, error)
Example
stack := NewLinkedStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*LinkedStack[T]) Push

func (x *LinkedStack[T]) Push(values ...T)
Example
stack := NewLinkedStack[int]()
stack.Push(1)

func (*LinkedStack[T]) Size

func (x *LinkedStack[T]) Size() int
Example
stack := NewLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*LinkedStack[T]) String

func (x *LinkedStack[T]) String() string
Example
stack := NewLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.String())
Output:

[1]

type MaxNode

type MaxNode[T any] struct {
	// contains filtered or unexported fields
}

type MaxStack

type MaxStack[T any] struct {
	// contains filtered or unexported fields
}

func NewMaxStack

func NewMaxStack[T any](comparator Comparator[T]) *MaxStack[T]
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.String())
Output:

[]

func (*MaxStack[T]) Clear

func (x *MaxStack[T]) Clear()
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*MaxStack[T]) GetMax

func (x *MaxStack[T]) GetMax() T
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
fmt.Println(stack.GetMax())
Output:

10

func (*MaxStack[T]) GetMaxE

func (x *MaxStack[T]) GetMaxE() (T, error)
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
10

func (*MaxStack[T]) IsEmpty

func (x *MaxStack[T]) IsEmpty() bool
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*MaxStack[T]) IsNotEmpty

func (x *MaxStack[T]) IsNotEmpty() bool
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*MaxStack[T]) Peek

func (x *MaxStack[T]) Peek() T
Example
type User struct {
}
stack := NewMaxStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*MaxStack[T]) PeekE

func (x *MaxStack[T]) PeekE() (T, error)
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*MaxStack[T]) Pop

func (x *MaxStack[T]) Pop() T
Example
type User struct {
}
stack := NewMaxStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*MaxStack[T]) PopE

func (x *MaxStack[T]) PopE() (T, error)
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*MaxStack[T]) Push

func (x *MaxStack[T]) Push(values ...T)
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)

func (*MaxStack[T]) Size

func (x *MaxStack[T]) Size() int
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*MaxStack[T]) String

func (x *MaxStack[T]) String() string
Example
stack := NewMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.String())
Output:

[&stack.MaxNode[int]{value:1, max:1}]

type MinNode

type MinNode[T any] struct {
	// contains filtered or unexported fields
}

type MinStack

type MinStack[T any] struct {
	// contains filtered or unexported fields
}

func NewMinStack

func NewMinStack[T any](comparator Comparator[T]) *MinStack[T]
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.String())
Output:

[]

func (*MinStack[T]) Clear

func (x *MinStack[T]) Clear()
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*MinStack[T]) GetMin

func (x *MinStack[T]) GetMin() T
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
fmt.Println(stack.GetMin())
Output:

7

func (*MinStack[T]) GetMinE

func (x *MinStack[T]) GetMinE() (T, error)
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
7

func (*MinStack[T]) IsEmpty

func (x *MinStack[T]) IsEmpty() bool
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*MinStack[T]) IsNotEmpty

func (x *MinStack[T]) IsNotEmpty() bool
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*MinStack[T]) Peek

func (x *MinStack[T]) Peek() T
Example
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*MinStack[T]) PeekE

func (x *MinStack[T]) PeekE() (T, error)
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*MinStack[T]) Pop

func (x *MinStack[T]) Pop() T
Example
type User struct {
}
stack := NewMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*MinStack[T]) PopE

func (x *MinStack[T]) PopE() (T, error)
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*MinStack[T]) Push

func (x *MinStack[T]) Push(values ...T)
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)

func (*MinStack[T]) Size

func (x *MinStack[T]) Size() int
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*MinStack[T]) String

func (x *MinStack[T]) String() string
Example
stack := NewMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.String())
Output:

[&stack.MinNode[int]{value:1, min:1}]

type Node

type Node[T any] struct {
	// contains filtered or unexported fields
}

Node 用于持有栈中元素的节点

type Stack

type Stack[T any] interface {

	// Push 往栈中加入元素,可以一次增加多个
	Push(values ...T)

	// Pop 弹出栈顶元素,如果栈为空的话返回栈元素T对应的零值
	Pop() T
	// PopE 弹出栈顶元素,如果栈为空的话则返回 ErrStackEmpty 错误
	PopE() (T, error)

	// Peek 访问但不弹出栈顶元素,如果栈为空的话则返回栈元素T对应的零值
	Peek() T
	// PeekE 访问但不弹出栈顶元素,如果栈为空的话则返回 ErrStackEmpty 错误
	PeekE() (T, error)

	// IsEmpty 栈是否为空
	IsEmpty() bool
	// IsNotEmpty 栈是否不为空
	IsNotEmpty() bool

	// Size 返回栈中元素的个数
	Size() int

	// Clear 清空栈
	Clear()

	// String 把栈打印为字符串,一般debug之类的可能会用得到
	String() string
}

Stack 定义栈的API

func NewStack

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

NewStack 默认是用数组实现的栈

type SyncArrayStack

type SyncArrayStack[T any] struct {
	// contains filtered or unexported fields
}

func NewSyncArrayStack

func NewSyncArrayStack[T any]() *SyncArrayStack[T]
Example
stack := NewSyncArrayStack[int]()
fmt.Println(stack.String())
Output:

[]

func (*SyncArrayStack[T]) Clear

func (x *SyncArrayStack[T]) Clear()
Example
stack := NewSyncArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*SyncArrayStack[T]) IsEmpty

func (x *SyncArrayStack[T]) IsEmpty() bool
Example
stack := NewSyncArrayStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*SyncArrayStack[T]) IsNotEmpty

func (x *SyncArrayStack[T]) IsNotEmpty() bool
Example
stack := NewSyncArrayStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*SyncArrayStack[T]) Peek

func (x *SyncArrayStack[T]) Peek() T
Example
type User struct {
}
stack := NewSyncArrayStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*SyncArrayStack[T]) PeekE

func (x *SyncArrayStack[T]) PeekE() (T, error)
Example
stack := NewSyncArrayStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncArrayStack[T]) Pop

func (x *SyncArrayStack[T]) Pop() T
Example
type User struct {
}
stack := NewSyncArrayStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*SyncArrayStack[T]) PopE

func (x *SyncArrayStack[T]) PopE() (T, error)
Example
stack := NewSyncArrayStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncArrayStack[T]) Push

func (x *SyncArrayStack[T]) Push(values ...T)
Example
stack := NewSyncArrayStack[int]()
stack.Push(1)

func (*SyncArrayStack[T]) Size

func (x *SyncArrayStack[T]) Size() int
Example
stack := NewSyncArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*SyncArrayStack[T]) String

func (x *SyncArrayStack[T]) String() string
Example
stack := NewSyncArrayStack[int]()
stack.Push(1)
fmt.Println(stack.String())
Output:

[1]

type SyncLinkedStack

type SyncLinkedStack[T any] struct {
	// contains filtered or unexported fields
}

func NewSyncLinkedStack

func NewSyncLinkedStack[T any]() *SyncLinkedStack[T]
Example
stack := NewSyncLinkedStack[int]()
fmt.Println(stack.String())
Output:

[]

func (*SyncLinkedStack[T]) Clear

func (x *SyncLinkedStack[T]) Clear()
Example
stack := NewSyncLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*SyncLinkedStack[T]) IsEmpty

func (x *SyncLinkedStack[T]) IsEmpty() bool
Example
stack := NewSyncLinkedStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*SyncLinkedStack[T]) IsNotEmpty

func (x *SyncLinkedStack[T]) IsNotEmpty() bool
Example
stack := NewSyncLinkedStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*SyncLinkedStack[T]) Peek

func (x *SyncLinkedStack[T]) Peek() T
Example
type User struct {
}
stack := NewSyncLinkedStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*SyncLinkedStack[T]) PeekE

func (x *SyncLinkedStack[T]) PeekE() (T, error)
Example
stack := NewSyncLinkedStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncLinkedStack[T]) Pop

func (x *SyncLinkedStack[T]) Pop() T
Example
type User struct {
}
stack := NewSyncLinkedStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*SyncLinkedStack[T]) PopE

func (x *SyncLinkedStack[T]) PopE() (T, error)
Example
stack := NewSyncLinkedStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncLinkedStack[T]) Push

func (x *SyncLinkedStack[T]) Push(values ...T)
Example
stack := NewSyncLinkedStack[int]()
stack.Push(1)

func (*SyncLinkedStack[T]) Size

func (x *SyncLinkedStack[T]) Size() int
Example
stack := NewSyncLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*SyncLinkedStack[T]) String

func (x *SyncLinkedStack[T]) String() string
Example
stack := NewSyncLinkedStack[int]()
stack.Push(1)
fmt.Println(stack.String())
Output:

[1]

type SyncMaxStack

type SyncMaxStack[T any] struct {
	// contains filtered or unexported fields
}

func NewSyncMaxStack

func NewSyncMaxStack[T any](comparator Comparator[T]) *SyncMaxStack[T]
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.String())
Output:

[]

func (*SyncMaxStack[T]) Clear

func (x *SyncMaxStack[T]) Clear()
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*SyncMaxStack[T]) GetMax

func (x *SyncMaxStack[T]) GetMax() T
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
fmt.Println(stack.GetMax())
Output:

10

func (*SyncMaxStack[T]) GetMaxE

func (x *SyncMaxStack[T]) GetMaxE() (T, error)
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
10

func (*SyncMaxStack[T]) IsEmpty

func (x *SyncMaxStack[T]) IsEmpty() bool
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*SyncMaxStack[T]) IsNotEmpty

func (x *SyncMaxStack[T]) IsNotEmpty() bool
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*SyncMaxStack[T]) Peek

func (x *SyncMaxStack[T]) Peek() T
Example
type User struct {
}
stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*SyncMaxStack[T]) PeekE

func (x *SyncMaxStack[T]) PeekE() (T, error)
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncMaxStack[T]) Pop

func (x *SyncMaxStack[T]) Pop() T
Example
type User struct {
}
stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*SyncMaxStack[T]) PopE

func (x *SyncMaxStack[T]) PopE() (T, error)
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncMaxStack[T]) Push

func (x *SyncMaxStack[T]) Push(values ...T)
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)

func (*SyncMaxStack[T]) Size

func (x *SyncMaxStack[T]) Size() int
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*SyncMaxStack[T]) String

func (x *SyncMaxStack[T]) String() string
Example
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.String())
Output:

[&stack.MaxNode[int]{value:1, max:1}]

type SyncMinStack

type SyncMinStack[T any] struct {
	// contains filtered or unexported fields
}

func NewSyncMinStack

func NewSyncMinStack[T any](comparator Comparator[T]) *SyncMinStack[T]
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.String())
Output:

[]

func (*SyncMinStack[T]) Clear

func (x *SyncMinStack[T]) Clear()
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
Output:

1
0

func (*SyncMinStack[T]) GetMin

func (x *SyncMinStack[T]) GetMin() T
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack.Push(10)
stack.Push(7)
stack.Push(9)
fmt.Println(stack.GetMin())
Output:

7

func (*SyncMinStack[T]) GetMinE

func (x *SyncMinStack[T]) GetMinE() (T, error)
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
7

func (*SyncMinStack[T]) IsEmpty

func (x *SyncMinStack[T]) IsEmpty() bool
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
Output:

true
false

func (*SyncMinStack[T]) IsNotEmpty

func (x *SyncMinStack[T]) IsNotEmpty() bool
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
Output:

false
true

func (*SyncMinStack[T]) Peek

func (x *SyncMinStack[T]) Peek() T
Example
type User struct {
}
stack := NewSyncMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
Output:

<nil>
&{}

func (*SyncMinStack[T]) PeekE

func (x *SyncMinStack[T]) PeekE() (T, error)
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncMinStack[T]) Pop

func (x *SyncMinStack[T]) Pop() T
Example
type User struct {
}
stack := NewSyncMinStack[*User](func(a, b *User) int { return 0 })
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
Output:

<nil>
&{}

func (*SyncMinStack[T]) PopE

func (x *SyncMinStack[T]) PopE() (T, error)
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
	fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(element)
Output:

stack empty!
1

func (*SyncMinStack[T]) Push

func (x *SyncMinStack[T]) Push(values ...T)
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)

func (*SyncMinStack[T]) Size

func (x *SyncMinStack[T]) Size() int
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.Size())
Output:

1

func (*SyncMinStack[T]) String

func (x *SyncMinStack[T]) String() string
Example
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack.Push(1)
fmt.Println(stack.String())
Output:

[&stack.MinNode[int]{value:1, min:1}]

Jump to

Keyboard shortcuts

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