# Stack
栈数据结构,包括ArrayStack(数组栈)和LinkedStack(链表栈)。
# 源码
- https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go (opens new window)
- https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go (opens new window)
# 用法
import (
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
# 目录
# 1. ArrayStack(数组栈)
# 2. LinkedStack(链表栈)
# 文档
# 1. ArrayStack
用切片实现栈结构
# NewArrayStack
返回ArrayStack指针实例
函数签名:
type ArrayStack[T any] struct {
data []T
length int
}
func NewArrayStack[T any]() *ArrayStack[T]
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk)
}
# Push
将元素加入数组栈
函数签名:
func (s *ArrayStack[T]) Push(value T)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Pop
删除栈顶元素并返回该元素指针
函数签名:
func (s *ArrayStack[T]) Pop() (*T, error)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
# Peak
返回栈顶元素指针
函数签名:
func (s *ArrayStack[T]) Peak() (*T, error)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Data
返回栈中所有元素组成的切片
函数签名:
func (s *ArrayStack[T]) Data() []T
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Size
返回栈中元素的数量
函数签名:
func (s *ArrayStack[T]) Size() int
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
# IsEmpty
判断栈是否为空
函数签名:
func (s *ArrayStack[T]) IsEmpty() bool
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
# Clear
清空栈元素,使栈为空
函数签名:
func (s *ArrayStack[T]) Clear()
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
# 2. LinkedStack
链表实现的栈结构。
# NewLinkedStack
返回LinkedStack指针实例
函数签名:
type StackNode[T any] struct {
Value T
Next *StackNode[T]
}
type LinkedStack[T any] struct {
top *datastructure.StackNode[T]
length int
}
func NewLinkedStack[T any]() *LinkedStack[T]
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk)
}
# Push
将元素加入链表栈
函数签名:
func (s *LinkedStack[T]) Push(value T)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Pop
删除栈顶元素并返回该元素指针
函数签名:
func (s *LinkedStack[T]) Pop() (*T, error)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
# Peak
返回栈顶元素指针
函数签名:
func (s *LinkedStack[T]) Peak() (*T, error)
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Data
返回栈中所有元素组成的切片
函数签名:
func (s *LinkedStack[T]) Data() []T
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
# Size
返回栈中元素的数量
函数签名:
func (s *LinkedStack[T]) Size() int
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
# IsEmpty
判断栈是否为空
函数签名:
func (s *LinkedStack[T]) IsEmpty() bool
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
# Clear
清空栈元素,使栈为空
函数签名:
func (s *LinkedStack[T]) Clear()
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
打印链表栈结构
函数签名:
func (s *LinkedStack[T]) Print()
例子:
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Print() //[ &{Value:3 Next:0xc000010260}, &{Value:2 Next:0xc000010250}, &{Value:1 Next:<nil>}, ]
}