# List

List 是线性表数据结构, 用 go 切片实现。

# 源码

# 用法

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

# 目录

# 文档

# NewList

返回List指针实例

函数签名:

type List[T any] struct {
	data []T
}
func NewList[T any](data []T) *List[T]

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    fmt.Println(li)
}

# Contain

判断列表中是否包含特定值

函数签名:

func (l *List[T]) Contain(value T) bool

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})

    fmt.Println(li.Contain(1)) //true
    fmt.Println(li.Contain(0)) //false
}

# Data

返回List中所有数据(切片)

函数签名:

func (l *List[T]) Data() []T

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    data := li.Data()

    fmt.Println(data) //[]int{1, 2, 3}
}

# ValueOf

返回列表中索引处的值指针

函数签名:

func (l *List[T]) ValueOf(index int) (*T, bool)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    v, ok := li.ValueOf(0)

    fmt.Println(*v) //1
    fmt.Println(ok) //true
}

# IndexOf

返回列表中值的索引,如果没有找到返回-1

函数签名:

func (l *List[T]) IndexOf(value T) int

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})

    fmt.Println(li.IndexOf(1)) //0
    fmt.Println(li.IndexOf(0)) //-1
}

# LastIndexOf

返回列表中最后一次出现的值的索引。如果未找到,则返回-1

函数签名:

func (l *List[T]) LastIndexOf(value T) int

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 1})

    fmt.Println(li.LastIndexOf(1)) // 3
    fmt.Println(li.LastIndexOf(0)) //-1
}

# IndexOfFunc

返回第一个符合函数条件的元素的索引。如果未找到,则返回-1

函数签名:

func (l *List[T]) IndexOfFunc(f func(T) bool) int

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})

    fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 1 })) //0
    fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 0 })) //-1
}

# LastIndexOfFunc

返回最后一个符合函数条件的元素的索引。如果未找到,则返回-1

函数签名:

func (l *List[T]) LastIndexOfFunc(f func(T) bool) int

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 1})

    fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3
    fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1
}

# Push

将值附加到列表末尾

函数签名:

func (l *List[T]) Push(value T)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    li.Push(4)

    fmt.Println(li.Data()) //[]int{1, 2, 3, 4}
}

# PopFirst

删除列表的第一个值并返回该值

函数签名:

func (l *List[T]) PopFirst() (*T, bool)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    v, ok := li.PopFirst()

    fmt.Println(*v) //1
    fmt.Println(ok) //true
    fmt.Println(li.Data()) //2, 3
}

# PopFirst

删除列表的最后一个值并返回该值

函数签名:

func (l *List[T]) PopLast() (*T, bool)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    v, ok := li.PopLast()

    fmt.Println(*v) //3
    fmt.Println(ok) //true
    fmt.Println(li.Data()) //1, 2
}

# DeleteAt

删除索引处列表的值,如果索引不在0和列表数据长度之间,则不执行任何操作

函数签名:

func (l *List[T]) DeleteAt(index int)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 4})

    li.DeleteAt(-1)
    fmt.Println(li.Data()) //1,2,3,4

    li.DeleteAt(4)
    fmt.Println(li.Data()) //1,2,3,4

    li.DeleteAt(0)
    fmt.Println(li.Data()) //2,3,4

    li.DeleteAt(2)
    fmt.Println(li.Data()) //2,3
}

# InsertAt

在索引处插入值到列表中,如果索引不在 0 和列表数据长度之间,则不执行任何操作

函数签名:

func (l *List[T]) InsertAt(index int, value T)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})

    li.InsertAt(-1, 0)
    fmt.Println(li.Data()) //1,2,3

    li.InsertAt(4, 0)
    fmt.Println(li.Data()) //1,2,3

    li.InsertAt(3, 4)
    fmt.Println(li.Data()) //1,2,3,4

    // li.InsertAt(2, 4)
    // fmt.Println(li.Data()) //1,2,4,3
}

# UpdateAt

更新索引处列表的值,索引应该在0和列表数据长度-1之间

函数签名:

func (l *List[T]) UpdateAt(index int, value T)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})

    li.UpdateAt(-1, 0)
    fmt.Println(li.Data()) //1,2,3

    li.UpdateAt(2, 4)
    fmt.Println(li.Data()) //1,2,4

    li.UpdateAt(3, 5)
    fmt.Println(li.Data()) //1,2,4
}

# Equal

比较一个列表和另一个列表,在每个元素上使用 reflect.DeepEqual

函数签名:

func (l *List[T]) Equal(other *List[T]) bool

例子:

package main

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

func main() {
    li1 := list.NewList([]int{1, 2, 3, 4})
    li2 := list.NewList([]int{1, 2, 3, 4})
    li3 := list.NewList([]int{1, 2, 3})

    fmt.Println(li1.Equal(li2)) //true
    fmt.Println(li1.Equal(li3)) //false
}

# IsEmpty

判断列表是否为空

函数签名:

func (l *List[T]) IsEmpty() bool

例子:

package main

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

func main() {
    li1 := list.NewList([]int{1, 2, 3})
    li2 := list.NewList([]int{})

    fmt.Println(li1.IsEmpty()) //false
    fmt.Println(li2.IsEmpty()) //true
}

# Clear

清空列表数据

函数签名:

func (l *List[T]) Clear()

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    li.Clear()

    fmt.Println(li.Data()) // empty
}

# Clone

返回列表的一个拷贝

函数签名:

func (l *List[T]) Clone() *List[T]

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3})
    cloneList := li.Clone()

    fmt.Println(cloneList.Data()) // 1,2,3
}

# Merge

合并两个列表,返回新的列表

函数签名:

func (l *List[T]) Merge(other *List[T]) *List[T]

例子:

package main

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

func main() {
    li1 := list.NewList([]int{1, 2, 3, 4})
    li2 := list.NewList([]int{4, 5, 6})
    li3 := li1.Merge(li2)

    fmt.Println(li3.Data()) //1, 2, 3, 4, 4, 5, 6
}

# Size

返回列表数据项的数量

函数签名:

func (l *List[T]) Size() int

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 4})

    fmt.Println(li.Size()) //4
}

# Cap

返回列表数据容量

函数签名:

func (l *List[T]) Cap() int

例子:

package main

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

func main() {
	data := make([]int, 0, 100)

    li := list.NewList(data)

    fmt.Println(li.Cap()) // 100
}

# Swap

交换列表中两个索引位置的值

函数签名:

func (l *List[T]) Swap(i, j int)

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 4})
    li.Swap(0, 3)

    fmt.Println(li.Data()) //4, 2, 3, 1
}

# Reverse

反转列表的数据项顺序

函数签名:

func (l *List[T]) Reverse()

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 3, 4})
    li.Reverse()

    fmt.Println(li.Data()) //4, 3, 2, 1
}

# Unique

列表去除重复数据项

函数签名:

func (l *List[T]) Unique()

例子:

package main

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

func main() {
    li := list.NewList([]int{1, 2, 2, 3, 4})
    li.Unique()

    fmt.Println(li.Data()) //1,2,3,4
}

# Union

两个列表取并集,去除重复数据项

函数签名:

func (l *List[T]) Union(other *List[T]) *List[T]

例子:

package main

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

func main() {
    li1 := list.NewList([]int{1, 2, 3, 4})
    li2 := list.NewList([]int{4, 5, 6})
    li3 := li1.Union(li2)

    fmt.Println(li3.Data()) //1,2,3,4,5,6
}

# Intersection

两个列表取交集

函数签名:

func (l *List[T]) Intersection(other *List[T]) *List[T]

例子:

package main

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

func main() {
    li1 := list.NewList([]int{1, 2, 3, 4})
    li2 := list.NewList([]int{4, 5, 6})
    li3 := li1.Intersection(li2)

    fmt.Println(li3.Data()) //4
}

# SubList

SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.

函数签名:

func (l *List[T]) SubList(fromIndex, toIndex int) *List[T]

例子:

package main

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

func main() {
    l := list.NewList([]int{1, 2, 3, 4, 5, 6})

    fmt.Println(l.SubList(2, 5)) // []int{3, 4, 5}
}

# DeleteIf

删除列表中所有符合函数(调用函数返回true)的元素,返回删除元素的数量

函数签名:

func (l *List[T]) DeleteIf(f func(T) bool) int

例子:

package main

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

func main() {
	l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})

	fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
	fmt.Println(l.Data()) // []int{2, 3, 4}
}
最后更新时间: 2022/10/22 下午1:42:08