# Linklist
Linklist是链表数据结构,它的节点有一个值和一个指向下一个节点的指针。
# 源码
- https://github.com/duke-git/lancet/blob/main/datastructure/link/singlylink.go (opens new window)
- https://github.com/duke-git/lancet/blob/main/datastructure/link/doublylink.go (opens new window)
# 用法
import (
link "github.com/duke-git/lancet/v2/datastructure/link"
)
# 目录
# 1. SinglyLink单链表
- NewSinglyLink
- Values
- InsertAt
- InsertAtHead
- InsertAtTail
- DeleteAt
- DeleteAtHead
- DeleteAtTail
- DeleteValue
- Reverse
- GetMiddleNode
- Size
- IsEmpty
- Clear
# 2. DoublyLink双向链表
- NewDoublyLink
- Values
- InsertAt
- InsertAtHead
- InsertAtTail
- DeleteAt
- DeleteAtHead
- DeleteAtTail
- Reverse
- GetMiddleNode
- Size
- IsEmpty
- Clear
# 文档
# 1. SinglyLink
SingleLink是单向链表,它的节点有一个值和一个指向链表的下一个节点的指针。
# NewSinglyLink
创建SinglyLink指针实例
函数签名:
type LinkNode[T any] struct {
Value T
Next *LinkNode[T]
}
type SinglyLink[T any] struct {
Head *datastructure.LinkNode[T]
length int
}
func NewSinglyLink[T any]() *SinglyLink[T]
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
fmt.Println(lk)
}
# Values
返回链表中所有节点值的切片
函数签名:
func (link *SinglyLink[T]) Values() []T
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# InsertAt
将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数
函数签名:
func (link *SinglyLink[T]) InsertAt(index int, value T) error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
lk.InsertAt(2, 4)
fmt.Println(lk.Values()) //[]int{1, 2, 4, 3}
}
# InsertAtHead
将值插入到链表表头
函数签名:
func (link *SinglyLink[T]) InsertAtHead(value T)
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtHead(1)
lk.InsertAtHead(2)
lk.InsertAtHead(3)
fmt.Println(lk.Values()) //[]int{3, 2, 1}
}
# InsertAtTail
将值插入到链表末尾
函数签名:
func (link *SinglyLink[T]) InsertAtTail(value T)
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# DeleteAt
删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数 - 1
函数签名:
func (link *SinglyLink[T]) DeleteAt(index int) error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# DeleteAtHead
删除链表头节点
函数签名:
func (link *SinglyLink[T]) DeleteAtHead() error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
# DeleteAtTail
删除链表末尾节点
函数签名:
func (link *SinglyLink[T]) DeleteAtTail() error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
# DeleteValue
删除链表中指定的value值
函数签名:
func (link *SinglyLink[T]) DeleteValue(value T)
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.DeleteValue(2)
fmt.Println(lk.Values()) //[]int{1, 3}
}
# Reverse
反转链表所有节点顺序
函数签名:
func (link *SinglyLink[T]) Reverse()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Reverse()
fmt.Println(lk.Values()) //[]int{3, 2, 1}
}
# GetMiddleNode
获取链表中部节点
函数签名:
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
midNode := lk.GetMiddleNode()
fmt.Println(midNode.Value) //2
}
# Size
获取链表节点数量
函数签名:
func (link *SinglyLink[T]) Size() int
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Size()) //3
}
# IsEmpty
判断链表是否为空
函数签名:
func (link *SinglyLink[T]) IsEmpty() bool
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
fmt.Println(lk.IsEmpty()) //true
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.IsEmpty()) //false
}
# Clear
清空链表所有节点
函数签名:
func (link *SinglyLink[T]) Clear()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Clear()
fmt.Println(lk.Values()) //
}
打印链表结构
函数签名:
func (link *SinglyLink[T]) Clear()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Print() //[ &{Value:1 Pre:<nil> Next:0xc0000a4048}, &{Value:2 Pre:<nil> Next:0xc0000a4060}, &{Value:3 Pre:<nil> Next:<nil>} ]
}
# 2. DoublyLink
DoublyLink是双向链表,它的节点有一个值,next指针指向下一个节点,pre指针指向前一个节点。
# NewDoublyLink
创建NewDoublyLink指针实例
函数签名:
type LinkNode[T any] struct {
Value T
Pre *LinkNode[T]
Next *LinkNode[T]
}
type DoublyLink[T any] struct {
Head *datastructure.LinkNode[T]
length int
}
func NewDoublyLink[T any]() *DoublyLink[T]
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
fmt.Println(lk)
}
# Values
返回链表中所有节点值的切片
函数签名:
func (link *DoublyLink[T]) Values() []T
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# InsertAt
将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数
函数签名:
func (link *DoublyLink[T]) InsertAt(index int, value T) error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
lk.InsertAt(2, 4)
fmt.Println(lk.Values()) //[]int{1, 2, 4, 3}
}
# InsertAtHead
将值插入到链表表头
函数签名:
func (link *DoublyLink[T]) InsertAtHead(value T)
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtHead(1)
lk.InsertAtHead(2)
lk.InsertAtHead(3)
fmt.Println(lk.Values()) //[]int{3, 2, 1}
}
# InsertAtTail
将值插入到链表末尾
函数签名:
func (link *DoublyLink[T]) InsertAtTail(value T)
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# DeleteAt
删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数 - 1
函数签名:
func (link *DoublyLink[T]) DeleteAt(index int) error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
# DeleteAtHead
删除链表头节点
函数签名:
func (link *DoublyLink[T]) DeleteAtHead() error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
# DeleteAtTail
删除链表末尾节点
函数签名:
func (link *DoublyLink[T]) DeleteAtTail() error
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
# Reverse
反转链表所有节点顺序
函数签名:
func (link *DoublyLink[T]) Reverse()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Reverse()
fmt.Println(lk.Values()) //[]int{3, 2, 1}
}
# GetMiddleNode
获取链表中部节点
函数签名:
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
midNode := lk.GetMiddleNode()
fmt.Println(midNode.Value) //2
}
# Size
获取链表节点数量
函数签名:
func (link *DoublyLink[T]) Size() int
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.Size()) //3
}
# IsEmpty
判断链表是否为空
函数签名:
func (link *DoublyLink[T]) IsEmpty() bool
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
fmt.Println(lk.IsEmpty()) //true
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
fmt.Println(lk.IsEmpty()) //false
}
# Clear
清空链表所有节点
函数签名:
func (link *DoublyLink[T]) Clear()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Clear()
fmt.Println(lk.Values()) //
}
打印链表结构
函数签名:
func (link *DoublyLink[T]) Clear()
例子:
package main
import (
"fmt"
link "github.com/duke-git/lancet/v2/datastructure/link"
)
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAtTail(1)
lk.InsertAtTail(2)
lk.InsertAtTail(3)
lk.Print() //
}