diff --git a/README.md b/README.md index 8894bc8..3f23fcf 100644 --- a/README.md +++ b/README.md @@ -394,16 +394,26 @@ import heap "github.com/duke-git/lancet/v2/datastructure/heap" import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" ``` -#### Function list: +#### Structure list: + +- **List** : a linear table, implemented with slice. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)] +- **Link** : link list structure, contains singly link and doubly link. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link.md)] +- **Stack** : stack structure(fifo), contains array stack and link stack. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md)] +- **Queue** : queue structure(filo), contains array queue, circular queue, link queue and priority queue. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue.md)] +- **Set** : a data container, like slice, but element of set is not duplicate. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set.md)] +- **Tree** : binary search tree structure. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree.md)] +- **Heap** : a binary max heap. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)] +- **Hashmap** : hash map structure. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)] + -- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md) -- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist.md) -- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md) -- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue.md) -- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set.md) -- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree.md) -- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md) -- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md) ### 8. Fileutil package implements some basic functions for file operations. diff --git a/README_zh-CN.md b/README_zh-CN.md index 62b3eae..7807a81 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -395,14 +395,25 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" #### Function list: -- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md) -- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist_zh-CN.md) -- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md) -- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue_zh-CN.md) -- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set_zh-CN.md) -- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree_zh-CN.md) -- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md) -- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md) +- **List** : 线性表结构, 用切片实现。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)] +- **Link** : 链表解构, 包括单链表和双向链表。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link_zh-CN.md)] +- **Stack** : 栈结构(fifo), 包括数组栈和链表栈。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md)] +- **Queue** : 队列结构(filo), 包括数组队列,链表队列,循环队列,优先级队列。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue_zh-CN.md)] +- **Set** : 集合(set)结构。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set_zh-CN.md)] +- **Tree** : 二叉搜索树。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree_zh-CN.md)] +- **Heap** : 二叉max堆。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap_zh-CN.md)] +- **Hashmap** : 哈希映射。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)] + + + ### 8. fileutil 包含文件基本操作。 diff --git a/datastructure/list/list.go b/datastructure/list/list.go index 199dd59..31687bd 100644 --- a/datastructure/list/list.go +++ b/datastructure/list/list.go @@ -8,17 +8,17 @@ import ( "reflect" ) -// List is a linear table, implemented with slice +// List is a linear table, implemented with slice. type List[T any] struct { data []T } -// NewList return a pointer of List +// NewList return a pointer of List. func NewList[T any](data []T) *List[T] { return &List[T]{data: data} } -// Data return list data +// Data return list data. func (l *List[T]) Data() []T { return l.data } @@ -31,7 +31,7 @@ func (l *List[T]) ValueOf(index int) (*T, bool) { return &l.data[index], true } -// IndexOf returns the index of value. if not found return -1 +// IndexOf returns the index of value. if not found return -1. func (l *List[T]) IndexOf(value T) int { index := -1 data := l.data @@ -45,7 +45,7 @@ func (l *List[T]) IndexOf(value T) int { } // LastIndexOf returns the index of the last occurrence of the value in this list. -// if not found return -1 +// if not found return -1. func (l *List[T]) LastIndexOf(value T) int { index := -1 data := l.data @@ -59,7 +59,7 @@ func (l *List[T]) LastIndexOf(value T) int { } // IndexOfFunc returns the first index satisfying f(v) -// if not found return -1 +// if not found return -1. func (l *List[T]) IndexOfFunc(f func(T) bool) int { index := -1 data := l.data @@ -73,7 +73,7 @@ func (l *List[T]) IndexOfFunc(f func(T) bool) int { } // LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i]) -// if not found return -1 +// if not found return -1. func (l *List[T]) LastIndexOfFunc(f func(T) bool) int { index := -1 data := l.data @@ -86,7 +86,7 @@ func (l *List[T]) LastIndexOfFunc(f func(T) bool) int { return index } -// Contain checks if the value in the list or not +// Contain checks if the value in the list or not. func (l *List[T]) Contain(value T) bool { data := l.data for _, v := range data { @@ -97,22 +97,22 @@ func (l *List[T]) Contain(value T) bool { return false } -// Push append value to the list data +// Push append value to the list data. func (l *List[T]) Push(value T) { l.data = append(l.data, value) } -// InsertAtFirst insert value into list at first index +// InsertAtFirst insert value into list at first index. func (l *List[T]) InsertAtFirst(value T) { l.InsertAt(0, value) } -// InsertAtLast insert value into list at last index +// InsertAtLast insert value into list at last index. func (l *List[T]) InsertAtLast(value T) { l.InsertAt(len(l.data), value) } -// InsertAt insert value into list at index +// InsertAt insert value into list at index. func (l *List[T]) InsertAt(index int, value T) { data := l.data size := len(data) @@ -123,7 +123,7 @@ func (l *List[T]) InsertAt(index int, value T) { l.data = append(data[:index], append([]T{value}, data[index:]...)...) } -// PopFirst delete the first value of list and return it +// PopFirst delete the first value of list and return it. func (l *List[T]) PopFirst() (*T, bool) { if len(l.data) == 0 { return nil, false @@ -135,7 +135,7 @@ func (l *List[T]) PopFirst() (*T, bool) { return &v, true } -// PopLast delete the last value of list and return it +// PopLast delete the last value of list and return it. func (l *List[T]) PopLast() (*T, bool) { size := len(l.data) if size == 0 { @@ -148,7 +148,7 @@ func (l *List[T]) PopLast() (*T, bool) { return &v, true } -// DeleteAt delete the value of list at index +// DeleteAt delete the value of list at index. func (l *List[T]) DeleteAt(index int) { data := l.data size := len(data) @@ -199,7 +199,7 @@ func (l *List[T]) UpdateAt(index int, value T) { l.data = append(data[:index], append([]T{value}, data[index+1:]...)...) } -// Equal compare list to other list, use reflect.DeepEqual +// Equal compare list to other list, use reflect.DeepEqual. func (l *List[T]) Equal(other *List[T]) bool { if len(l.data) != len(other.data) { return false @@ -214,17 +214,17 @@ func (l *List[T]) Equal(other *List[T]) bool { return true } -// IsEmpty check if the list is empty or not +// IsEmpty check if the list is empty or not. func (l *List[T]) IsEmpty() bool { return len(l.data) == 0 } -// Clear the data of list +// Clear the data of list. func (l *List[T]) Clear() { l.data = make([]T, 0) } -// Clone return a copy of list +// Clone return a copy of list. func (l *List[T]) Clone() *List[T] { cl := NewList(make([]T, len(l.data))) copy(cl.data, l.data) @@ -232,7 +232,7 @@ func (l *List[T]) Clone() *List[T] { return cl } -// Merge two list, return new list, don't change original list +// Merge two list, return new list, don't change original list. func (l *List[T]) Merge(other *List[T]) *List[T] { l1, l2 := len(l.data), len(other.data) ml := NewList(make([]T, l1+l2)) @@ -243,17 +243,17 @@ func (l *List[T]) Merge(other *List[T]) *List[T] { return ml } -// Size return number of list data items +// Size return number of list data items. func (l *List[T]) Size() int { return len(l.data) } -// Cap return cap of the inner data +// Cap return cap of the inner data. func (l *List[T]) Cap() int { return cap(l.data) } -// Swap the value of index i and j in list +// Swap the value of index i and j in list. func (l *List[T]) Swap(i, j int) { size := len(l.data) if i < 0 || i >= size || j < 0 || j >= size { @@ -262,14 +262,14 @@ func (l *List[T]) Swap(i, j int) { l.data[i], l.data[j] = l.data[j], l.data[i] } -// Reverse the item order of list +// Reverse the item order of list. func (l *List[T]) Reverse() { for i, j := 0, len(l.data)-1; i < j; i, j = i+1, j-1 { l.data[i], l.data[j] = l.data[j], l.data[i] } } -// Unique remove duplicate items in list +// Unique remove duplicate items in list. func (l *List[T]) Unique() { data := l.data size := len(data) @@ -303,7 +303,7 @@ func (l *List[T]) Union(other *List[T]) *List[T] { return result } -// Intersection creates a new list whose element both be contained in list l and other +// Intersection creates a new list whose element both be contained in list l and other. func (l *List[T]) Intersection(other *List[T]) *List[T] { result := NewList(make([]T, 0)) diff --git a/docs/datastructure/linklist.md b/docs/datastructure/linklist.md deleted file mode 100644 index 174ccff..0000000 --- a/docs/datastructure/linklist.md +++ /dev/null @@ -1,1018 +0,0 @@ -# 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/linklist_zh-CN.md b/docs/datastructure/linklist_zh-CN.md deleted file mode 100644 index f7bc2e4..0000000 --- a/docs/datastructure/linklist_zh-CN.md +++ /dev/null @@ -1,1017 +0,0 @@ -# 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