diff --git a/docs/datastructure/link.md b/docs/datastructure/link.md new file mode 100644 index 0000000..174ccff --- /dev/null +++ b/docs/datastructure/link.md @@ -0,0 +1,1018 @@ +# Linklist +Linklist a linked list, whose node has a value and a pointer points to next node of the link. + +
+ +## Source + +- [https://github.com/duke-git/lancet/blob/main/datastructure/link/singlylink.go](https://github.com/duke-git/lancet/blob/main/datastructure/link/singlylink.go) +- [https://github.com/duke-git/lancet/blob/main/datastructure/link/doublylink.go](https://github.com/duke-git/lancet/blob/main/datastructure/link/doublylink.go) + + + + +## Usage +```go +import ( + link "github.com/duke-git/lancet/v2/datastructure/link" +) +``` + + + +## Index + +### 1. SinglyLink + +- [NewSinglyLink](#NewSinglyLink) +- [Values](#SinglyLink_Values) +- [InsertAt](#SinglyLink_InsertAt) +- [InsertAtHead](#SinglyLink_InsertAtHead) +- [InsertAtTail](#SinglyLink_InsertAtTail) +- [DeleteAt](#SinglyLink_DeleteAt) +- [DeleteAtHead](#SinglyLink_DeleteAtHead) +- [DeleteAtTail](#SinglyLink_DeleteAtTail) +- [DeleteValue](#SinglyLink_DeleteValue) +- [Reverse](#SinglyLink_Reverse) +- [GetMiddleNode](#SinglyLink_GetMiddleNode) +- [Size](#SinglyLink_Size) +- [IsEmpty](#SinglyLink_IsEmpty) +- [Clear](#SinglyLink_Clear) +- [Print](#SinglyLink_Print) + +### 2. DoublyLink + +- [NewDoublyLink](#NewDoublyLink) +- [Values](#DoublyLink_Values) +- [InsertAt](#DoublyLink_InsertAt) +- [InsertAtHead](#DoublyLink_InsertAtHead) +- [InsertAtTail](#DoublyLink_InsertAtTail) +- [DeleteAt](#DoublyLink_DeleteAt) +- [DeleteAtHead](#DoublyLink_DeleteAtHead) +- [DeleteAtTail](#DoublyLink_DeleteAtTail) +- [Reverse](#DoublyLink_Reverse) +- [GetMiddleNode](#DoublyLink_GetMiddleNode) +- [Size](#DoublyLink_Size) +- [IsEmpty](#DoublyLink_IsEmpty) +- [Clear](#DoublyLink_Clear) +- [Print](#DoublyLink_Print) + + + + +## Documentation + +### 1. SinglyLink +SinglyLink a linked list, whose node has a value and a pointer points to next node of the link. + +### NewSinglyLink +Return a singly link(SinglyLink) instance
+ +Signature: + +```go +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] +``` +Example: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewSinglyLink[int]() + fmt.Println(lk) +} +``` + + + +### Values +Return a slice of all node value in singly linklist
+ +Signature: + +```go +func (link *SinglyLink[T]) Values() []T +``` +Example: + +```go +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 +Insert value into singly linklist at index, param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing
+ +Signature: + +```go +func (link *SinglyLink[T]) InsertAt(index int, value T) +``` +Example: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewSinglyLink[int]() + + lk.InsertAt(1, 1) //do nothing + + 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 +Insert value into singly linklist at head(first) index
+ +Signature: + +```go +func (link *SinglyLink[T]) InsertAtHead(value T) +``` +Example: + +```go +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 +Insert value into singly linklist at tail(last) index
+ +Signature: + +```go +func (link *SinglyLink[T]) InsertAtTail(value T) +``` +Example: + +```go +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 +Delete value at specific index, param `index` should be [0, len(SinglyLink)-1]
+ +Signature: + +```go +func (link *SinglyLink[T]) DeleteAt(index int) +``` +Example: + +```go +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) + + lk.DeleteAt(3) + + fmt.Println(lk.Values()) //[]int{1, 2, 3} +} +``` + + + +### DeleteAtHead +Delete value in singly linklist at first index
+ +Signature: + +```go +func (link *SinglyLink[T]) DeleteAtHead() +``` +Example: + +```go +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) + + lk.DeleteAtHead() + + fmt.Println(lk.Values()) //[]int{2, 3, 4} +} +``` + + + + +### DeleteAtTail +Delete value in singly linklist at last index
+ +Signature: + +```go +func (link *SinglyLink[T]) DeleteAtTail() +``` +Example: + +```go +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.DeleteAtTail() + + fmt.Println(lk.Values()) //[]int{1, 2} +} +``` + + + +### DeleteValue +Delete all `value` in singly linklist
+ +Signature: + +```go +func (link *SinglyLink[T]) DeleteValue(value T) +``` +Example: + +```go +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 +Reverse all nodes order in linkist
+ +Signature: + +```go +func (link *SinglyLink[T]) Reverse() +``` +Example: + +```go +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 +Get the node at middle index of linkist
+ +Signature: + +```go +func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] +``` +Example: + +```go +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 +Get the number of nodes in linklist
+ +Signature: + +```go +func (link *SinglyLink[T]) Size() int +``` +Example: + +```go +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 +Checks if linklist is empty or not
+ +Signature: + +```go +func (link *SinglyLink[T]) IsEmpty() bool +``` +Example: + +```go +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 +Clear all nodes in the linklist, make it empty
+ +Signature: + +```go +func (link *SinglyLink[T]) Clear() +``` +Example: + +```go +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()) // +} +``` + + + +### Print +Print all nodes info of linklist
+ +Signature: + +```go +func (link *SinglyLink[T]) Clear() +``` +Example: + +```go +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:Return a doubly link instance
+ +Signature: + +```go +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] +``` +Example: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewDoublyLink[int]() + fmt.Println(lk) +} +``` + + + +### Values +Return a slice of all node value in doubly linklist
+ +Signature: + +```go +func (link *DoublyLink[T]) Values() []T +``` +Example: + +```go +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 +Insert value into doubly linklist at index, param `index` should between [0, len(DoublyLink)], if index do not meet the conditions, do nothing
+ +Signature: + +```go +func (link *DoublyLink[T]) InsertAt(index int, value T) +``` +Example: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewDoublyLink[int]() + + lk.InsertAt(1, 1) //do nothing + + 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 +Insert value into doubly linklist at head(first) index
+ +Signature: + +```go +func (link *DoublyLink[T]) InsertAtHead(value T) +``` +Example: + +```go +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 +Insert value into doubly linklist at tail(last) index
+ +Signature: + +```go +func (link *DoublyLink[T]) InsertAtTail(value T) +``` +Example: + +```go +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 +Delete value at specific index, param `index` should be [0, len(DoublyLink)-1]
+ +Signature: + +```go +func (link *DoublyLink[T]) DeleteAt(index int) +``` +Example: + +```go +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) + + lk.DeleteAt(3) + + fmt.Println(lk.Values()) //[]int{1, 2, 3} +} +``` + + + +### DeleteAtHead +Delete value in doubly linklist at first index
+ +Signature: + +```go +func (link *DoublyLink[T]) DeleteAtHead() +``` +Example: + +```go +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) + + lk.DeleteAtHead() + + fmt.Println(lk.Values()) //[]int{2, 3, 4} +} +``` + + + + +### DeleteAtTail +Delete value in doubly linklist at last index
+ +Signature: + +```go +func (link *DoublyLink[T]) DeleteAtTail() error +``` +Example: + +```go +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 +Reverse all nodes order in linkist
+ +Signature: + +```go +func (link *DoublyLink[T]) Reverse() +``` +Example: + +```go +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 +Get the node at middle index of linkist
+ +Signature: + +```go +func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] +``` +Example: + +```go +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 +Get the number of nodes in linklist
+ +Signature: + +```go +func (link *DoublyLink[T]) Size() int +``` +Example: + +```go +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 +Checks if linklist is empty or not
+ +Signature: + +```go +func (link *DoublyLink[T]) IsEmpty() bool +``` +Example: + +```go +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 +Clear all nodes in the linklist, make it empty
+ +Signature: + +```go +func (link *DoublyLink[T]) Clear() +``` +Example: + +```go +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()) // +} +``` + + + +### Print +Print all nodes info of linklist
+ +Signature: + +```go +func (link *DoublyLink[T]) Clear() +``` +Example: + +```go +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() // +} +``` \ No newline at end of file diff --git a/docs/datastructure/link_zh-CN.md b/docs/datastructure/link_zh-CN.md new file mode 100644 index 0000000..f7bc2e4 --- /dev/null +++ b/docs/datastructure/link_zh-CN.md @@ -0,0 +1,1017 @@ +# Linklist +Linklist是链表数据结构,它的节点有一个值和一个指向下一个节点的指针。 + + + +## 源码 + +- [https://github.com/duke-git/lancet/blob/main/datastructure/link/singlylink.go](https://github.com/duke-git/lancet/blob/main/datastructure/link/singlylink.go) +- [https://github.com/duke-git/lancet/blob/main/datastructure/link/doublylink.go](https://github.com/duke-git/lancet/blob/main/datastructure/link/doublylink.go) + + + + +## 用法 +```go +import ( + link "github.com/duke-git/lancet/v2/datastructure/link" +) +``` + + + +## 目录 + +### 1. SinglyLink单链表 + +- [NewSinglyLink](#NewSinglyLink) +- [Values](#SinglyLink_Values) +- [InsertAt](#SinglyLink_InsertAt) +- [InsertAtHead](#SinglyLink_InsertAtHead) +- [InsertAtTail](#SinglyLink_InsertAtTail) +- [DeleteAt](#SinglyLink_DeleteAt) +- [DeleteAtHead](#SinglyLink_DeleteAtHead) +- [DeleteAtTail](#SinglyLink_DeleteAtTail) +- [DeleteValue](#SinglyLink_DeleteValue) +- [Reverse](#SinglyLink_Reverse) +- [GetMiddleNode](#SinglyLink_GetMiddleNode) +- [Size](#SinglyLink_Size) +- [IsEmpty](#SinglyLink_IsEmpty) +- [Clear](#SinglyLink_Clear) +- [Print](#SinglyLink_Print) + +### 2. DoublyLink双向链表 + +- [NewDoublyLink](#NewDoublyLink) +- [Values](#DoublyLink_Values) +- [InsertAt](#DoublyLink_InsertAt) +- [InsertAtHead](#DoublyLink_InsertAtHead) +- [InsertAtTail](#DoublyLink_InsertAtTail) +- [DeleteAt](#DoublyLink_DeleteAt) +- [DeleteAtHead](#DoublyLink_DeleteAtHead) +- [DeleteAtTail](#DoublyLink_DeleteAtTail) +- [Reverse](#DoublyLink_Reverse) +- [GetMiddleNode](#DoublyLink_GetMiddleNode) +- [Size](#DoublyLink_Size) +- [IsEmpty](#DoublyLink_IsEmpty) +- [Clear](#DoublyLink_Clear) +- [Print](#DoublyLink_Print) + + + + +## 文档 + +### 1. SinglyLink +SingleLink是单向链表,它的节点有一个值和一个指向链表的下一个节点的指针。 + +### NewSinglyLink +创建SinglyLink指针实例
+ +函数签名: + +```go +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] +``` +例子: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewSinglyLink[int]() + fmt.Println(lk) +} +``` + + + +### Values +返回链表中所有节点值的切片
+ +函数签名: + +```go +func (link *SinglyLink[T]) Values() []T +``` +例子: + +```go +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且小于或等于链表节点数
+ +函数签名: + +```go +func (link *SinglyLink[T]) InsertAt(index int, value T) +``` +例子: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewSinglyLink[int]() + + lk.InsertAt(1, 1) //do nothing + + 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 +将值插入到链表表头
+ +函数签名: + +```go +func (link *SinglyLink[T]) InsertAtHead(value T) +``` +例子: + +```go +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 +将值插入到链表末尾
+ +函数签名: + +```go +func (link *SinglyLink[T]) InsertAtTail(value T) +``` +例子: + +```go +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
+ +函数签名: + +```go +func (link *SinglyLink[T]) DeleteAt(index int) +``` +例子: + +```go +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) + + lk.DeleteAt(3) + + fmt.Println(lk.Values()) //[]int{1, 2, 3} +} +``` + + + +### DeleteAtHead +删除链表头节点
+ +函数签名: + +```go +func (link *SinglyLink[T]) DeleteAtHead() +``` +例子: + +```go +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) + + lk.DeleteAtHead() + + fmt.Println(lk.Values()) //[]int{2, 3, 4} +} +``` + + + + +### DeleteAtTail +删除链表末尾节点
+ +函数签名: + +```go +func (link *SinglyLink[T]) DeleteAtTail() +``` +例子: + +```go +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.DeleteAtTail() + + fmt.Println(lk.Values()) //[]int{1, 2} +} +``` + + + +### DeleteValue +删除链表中指定的value值
+ +函数签名: + +```go +func (link *SinglyLink[T]) DeleteValue(value T) +``` +例子: + +```go +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 +反转链表所有节点顺序
+ +函数签名: + +```go +func (link *SinglyLink[T]) Reverse() +``` +例子: + +```go +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 +获取链表中部节点
+ +函数签名: + +```go +func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] +``` +例子: + +```go +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 +获取链表节点数量
+ +函数签名: + +```go +func (link *SinglyLink[T]) Size() int +``` +例子: + +```go +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 +判断链表是否为空
+ +函数签名: + +```go +func (link *SinglyLink[T]) IsEmpty() bool +``` +例子: + +```go +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 +清空链表所有节点
+ +函数签名: + +```go +func (link *SinglyLink[T]) Clear() +``` +例子: + +```go +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()) // +} +``` + + + +### Print +打印链表结构
+ +函数签名: + +```go +func (link *SinglyLink[T]) Clear() +``` +例子: + +```go +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:创建NewDoublyLink指针实例
+ +函数签名: + +```go +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] +``` +例子: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewDoublyLink[int]() + fmt.Println(lk) +} +``` + + + +### Values +返回链表中所有节点值的切片
+ +函数签名: + +```go +func (link *DoublyLink[T]) Values() []T +``` +例子: + +```go +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且小于或等于链表节点数
+ +函数签名: + +```go +func (link *DoublyLink[T]) InsertAt(index int, value T) +``` +例子: + +```go +package main + +import ( + "fmt" + link "github.com/duke-git/lancet/v2/datastructure/link" +) + +func main() { + lk := link.NewDoublyLink[int]() + + lk.InsertAt(1, 1) //do nothing + + 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 +将值插入到链表表头
+ +函数签名: + +```go +func (link *DoublyLink[T]) InsertAtHead(value T) +``` +例子: + +```go +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 +将值插入到链表末尾
+ +函数签名: + +```go +func (link *DoublyLink[T]) InsertAtTail(value T) +``` +例子: + +```go +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
+ +函数签名: + +```go +func (link *DoublyLink[T]) DeleteAt(index int) +``` +例子: + +```go +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) + + lk.DeleteAt(3) + + fmt.Println(lk.Values()) //[]int{1, 2, 3} +} +``` + + + +### DeleteAtHead +删除链表头节点
+ +函数签名: + +```go +func (link *DoublyLink[T]) DeleteAtHead() +``` +例子: + +```go +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) + + lk.DeleteAtHead() + + fmt.Println(lk.Values()) //[]int{2, 3, 4} +} +``` + + + + +### DeleteAtTail +删除链表末尾节点
+ +函数签名: + +```go +func (link *DoublyLink[T]) DeleteAtTail() +``` +例子: + +```go +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.DeleteAtTail() + + fmt.Println(lk.Values()) //[]int{1, 2} +} +``` + + + + +### Reverse +反转链表所有节点顺序
+ +函数签名: + +```go +func (link *DoublyLink[T]) Reverse() +``` +例子: + +```go +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 +获取链表中部节点
+ +函数签名: + +```go +func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] +``` +例子: + +```go +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 +获取链表节点数量
+ +函数签名: + +```go +func (link *DoublyLink[T]) Size() int +``` +例子: + +```go +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 +判断链表是否为空
+ +函数签名: + +```go +func (link *DoublyLink[T]) IsEmpty() bool +``` +例子: + +```go +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 +清空链表所有节点
+ +函数签名: + +```go +func (link *DoublyLink[T]) Clear() +``` +例子: + +```go +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()) // +} +``` + + + +### Print +打印链表结构
+ +函数签名: + +```go +func (link *DoublyLink[T]) Clear() +``` +例子: + +```go +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() // +} +``` \ No newline at end of file