# List List is a linear table, implemented with slice.
## Source - [https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go) ## Usage ```go import ( list "github.com/duke-git/lancet/v2/datastructure/list" ) ``` ## Index - [NewList](#NewList) - [Contain](#Contain) - [Data](#Data) - [ValueOf](#ValueOf) - [IndexOf](#IndexOf) - [Push](#Push) - [PopFirst](#PopFirst) - [PopLast](#PopLast) - [DeleteAt](#DeleteAt) - [InsertAt](#InsertAt) - [UpdateAt](#UpdateAt) - [Equal](#Equal) - [IsEmpty](#IsEmpty) - [Clear](#Clear) - [Clone](#Clone) - [Merge](#Merge) - [Size](#Size) - [Swap](#Swap) - [Reverse](#Reverse) - [Unique](#Unique) - [Union](#Union) - [Intersection](#Intersection) ## Documentation ### NewListList is a linear table, implemented with slice. NewList function return a list pointer
Signature: ```go type List[T any] struct { data []T } func NewList[T any](data []T) *List[T] ``` Example: ```go 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) } ``` ### ContainCheck if the value in the list or not
Signature: ```go func (l *List[T]) Contain(value T) bool ``` Example: ```go 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 } ``` ### DataReturn slice of list data
Signature: ```go func (l *List[T]) Data() []T ``` Example: ```go 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} } ``` ### ValueOfReturn the value pointer at index in list
Signature: ```go func (l *List[T]) ValueOf(index int) (*T, bool) ``` Example: ```go 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 } ``` ### IndexOfReture the index of value in the list. if not found return -1
Signature: ```go func (l *List[T]) IndexOf(value T) int ``` Example: ```go 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 } ``` ### PushAppend value to the list
Signature: ```go func (l *List[T]) Push(value T) ``` Example: ```go 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} } ``` ### PopFirstDelete the first value of list and return it
Signature: ```go func (l *List[T]) PopFirst() (*T, bool) ``` Example: ```go 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 } ``` ### PopFirstDelete the last value of list and return it
Signature: ```go func (l *List[T]) PopLast() (*T, bool) ``` Example: ```go 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 } ``` ### DeleteAtDelete the value of list at index, if index is not between 0 and length of list data, do nothing
Signature: ```go func (l *List[T]) DeleteAt(index int) ``` Example: ```go 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 } ``` ### InsertAtInsert value into list at index, if index is not between 0 and length of list data, do nothing
Signature: ```go func (l *List[T]) InsertAt(index int, value T) ``` Example: ```go 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 } ``` ### UpdateAtUpdate value of list at index, index shoud between 0 and list size - 1
Signature: ```go func (l *List[T]) UpdateAt(index int, value T) ``` Example: ```go 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 } ``` ### EqualCompare a list to another list, use reflect.DeepEqual on every element
Signature: ```go func (l *List[T]) Equal(other *List[T]) bool ``` Example: ```go 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 } ``` ### IsEmptyCheck if a list is empty or not
Signature: ```go func (l *List[T]) IsEmpty() bool ``` Example: ```go 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 } ``` ### ClearClear the data of list
Signature: ```go func (l *List[T]) Clear() ``` Example: ```go 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 } ``` ### CloneReturn a copy of list
Signature: ```go func (l *List[T]) Clone() *List[T] ``` Example: ```go 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 } ``` ### MergeMerge two list, return new list, don't change original list
Signature: ```go func (l *List[T]) Merge(other *List[T]) *List[T] ``` Example: ```go 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 } ``` ### SizeReturn number of list data items
Signature: ```go func (l *List[T]) Size() int ``` Example: ```go 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 } ``` ### SwapSwap the value at two index in list
Signature: ```go func (l *List[T]) Swap(i, j int) ``` Example: ```go 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 } ``` ### ReverseReverse the data item order of list
Signature: ```go func (l *List[T]) Reverse() ``` Example: ```go 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 } ``` ### UniqueRemove duplicate items in list
Signature: ```go func (l *List[T]) Unique() ``` Example: ```go 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 } ``` ### UnionCreates a new list contain all elements in list l and other, remove duplicate element
Signature: ```go func (l *List[T]) Union(other *List[T]) *List[T] ``` Example: ```go 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 } ``` ### IntersectionCreates a new list whose element both be contained in list l and other
Signature: ```go func (l *List[T]) Intersection(other *List[T]) *List[T] ``` Example: ```go 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 } ```