Skip to content
On this page

# 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/doublylink.go

## Usage ​

go

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

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

## Index ​

### 1. SinglyLink ​

- NewSinglyLink
- Values
- InsertAt
- InsertAtHead
- InsertAtTail
- DeleteAt
- DeleteAtHead
- DeleteAtTail
- DeleteValue
- Reverse
- GetMiddleNode
- Size
- IsEmpty
- Clear
- Print

### 2. DoublyLink ​

- NewDoublyLink
- Values
- InsertAt
- InsertAtHead
- InsertAtTail
- DeleteAt
- DeleteAtHead
- DeleteAtTail
- Reverse
- GetMiddleNode
- Size
- IsEmpty
- Clear
- 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]

    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)
    }

    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

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

    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)

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

    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)

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

    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)

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

    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)

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

    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()

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

    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()

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

    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)

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

    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()

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

    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]

    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
    }

    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

    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
    }

    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

    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
    }

    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()

    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()) //
    }

    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()

    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:<nil> Next:0xc0000a4048}, &{Value:2 Pre:<nil> Next:0xc0000a4060}, &{Value:3 Pre:<nil> Next:<nil>} ]
    }

    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 is a linked list, whose node has a value, a next pointer points to next node and pre pointer points to previous node of the link.

### NewDoublyLink ​

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]

    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)
    }

    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

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

    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)

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

    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)

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

    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)

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

    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)

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

    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()

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

    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

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

    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()

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

    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]

    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
    }

    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

    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
    }

    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

    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
    }

    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()

    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()) //
    }

    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()

    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() //
    }

    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() //
    }