Skip to content

Stack

栈数据结构,包括ArrayStack(数组栈)和LinkedStack(链表栈)。

源码

用法

go
import (
    stack "github.com/duke-git/lancet/v2/datastructure/stack"
)

目录

1. ArrayStack(数组栈)

2. LinkedStack(链表栈)

文档

1. ArrayStack

用切片实现栈结构

NewArrayStack

返回ArrayStack指针实例

函数签名:

go
type ArrayStack[T any] struct {
	data   []T
	length int
}
func NewArrayStack[T any]() *ArrayStack[T]

示例:

go
package main

import (
    "fmt"
    stack "github.com/duke-git/lancet/v2/datastructure/stack"
)

func main() {
    sk := stack.NewArrayStack[int]()
    fmt.Println(sk)
}

Push

将元素加入数组栈

函数签名:

go
func (s *ArrayStack[T]) Push(value T)

示例:

go
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

删除栈顶元素并返回该元素指针

函数签名:

go
func (s *ArrayStack[T]) Pop() (*T, error)

示例:

go
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

返回栈顶元素指针

函数签名:

go
func (s *ArrayStack[T]) Peak() (*T, error)

示例:

go
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

返回栈中所有元素组成的切片

函数签名:

go
func (s *ArrayStack[T]) Data() []T

示例:

go
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

返回栈中元素的数量

函数签名:

go
func (s *ArrayStack[T]) Size() int

示例:

go
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

判断栈是否为空

函数签名:

go
func (s *ArrayStack[T]) IsEmpty() bool

示例:

go
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

清空栈元素,使栈为空

函数签名:

go
func (s *ArrayStack[T]) Clear()

示例:

go
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指针实例

函数签名:

go
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]

示例:

go
package main

import (
    "fmt"
    stack "github.com/duke-git/lancet/v2/datastructure/stack"
)

func main() {
    sk := stack.NewLinkedStack[int]()
    fmt.Println(sk)
}

Push

将元素加入链表栈

函数签名:

go
func (s *LinkedStack[T]) Push(value T)

示例:

go
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

删除栈顶元素并返回该元素指针

函数签名:

go
func (s *LinkedStack[T]) Pop() (*T, error)

示例:

go
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

返回栈顶元素指针

函数签名:

go
func (s *LinkedStack[T]) Peak() (*T, error)

示例:

go
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

返回栈中所有元素组成的切片

函数签名:

go
func (s *LinkedStack[T]) Data() []T

示例:

go
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

返回栈中元素的数量

函数签名:

go
func (s *LinkedStack[T]) Size() int

示例:

go
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

判断栈是否为空

函数签名:

go
func (s *LinkedStack[T]) IsEmpty() bool

示例:

go
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

清空栈元素,使栈为空

函数签名:

go
func (s *LinkedStack[T]) Clear()

示例:

go
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{}
}

Print

打印链表栈结构

函数签名:

go
func (s *LinkedStack[T]) Print()

示例:

go
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>},  ]
}