# 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. ### NewSinglyLinkReturn 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) } ``` ### ValuesReturn 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} } ``` ### InsertAtInsert value into singly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes
Signature: ```go func (link *SinglyLink[T]) InsertAt(index int, value T) error ``` Example: ```go 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} } ``` ### InsertAtHeadInsert 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} } ``` ### InsertAtTailInsert 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} } ``` ### DeleteAtDelete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1
Signature: ```go func (link *SinglyLink[T]) DeleteAt(index int) error ``` 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) err := lk.DeleteAt(3) fmt.Println(err) //nil fmt.Println(lk.Values()) //[]int{1, 2, 3} } ``` ### DeleteAtHeadDelete value in singly linklist at first index
Signature: ```go func (link *SinglyLink[T]) DeleteAtHead() error ``` 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) err := lk.DeleteAtHead() fmt.Println(err) //nil fmt.Println(lk.Values()) //[]int{2, 3, 4} } ``` ### DeleteAtTailDelete value in singly linklist at last index
Signature: ```go func (link *SinglyLink[T]) DeleteAtTail() error ``` 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) err := lk.DeleteAtTail() fmt.Println(err) //nil fmt.Println(lk.Values()) //[]int{1, 2} } ``` ### DeleteValueDelete 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} } ``` ### ReverseReverse 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} } ``` ### GetMiddleNodeGet 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 } ``` ### SizeGet 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 } ``` ### IsEmptyChecks 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 } ``` ### ClearClear 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()) // } ``` ### PrintPrint 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) } ``` ### ValuesReturn 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} } ``` ### InsertAtInsert value into doubly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes
Signature: ```go func (link *DoublyLink[T]) InsertAt(index int, value T) error ``` Example: ```go 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} } ``` ### InsertAtHeadInsert 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} } ``` ### InsertAtTailInsert 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} } ``` ### DeleteAtDelete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1
Signature: ```go func (link *DoublyLink[T]) DeleteAt(index int) 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) lk.InsertAtTail(4) err := lk.DeleteAt(3) fmt.Println(err) //nil fmt.Println(lk.Values()) //[]int{1, 2, 3} } ``` ### DeleteAtHeadDelete value in doubly linklist at first index
Signature: ```go func (link *DoublyLink[T]) DeleteAtHead() 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) lk.InsertAtTail(4) err := lk.DeleteAtHead() fmt.Println(err) //nil fmt.Println(lk.Values()) //[]int{2, 3, 4} } ``` ### DeleteAtTailDelete 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} } ``` ### ReverseReverse 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} } ``` ### GetMiddleNodeGet 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 } ``` ### SizeGet 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 } ``` ### IsEmptyChecks 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 } ``` ### ClearClear 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()) // } ``` ### PrintPrint 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() // } ```