mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
list
This commit is contained in:
@ -15,9 +15,16 @@ type LinkedNode[T any] struct {
|
||||
}
|
||||
|
||||
func NewLinkedList[T any](elems ...T) *LinkedList[T] {
|
||||
return &LinkedList[T]{
|
||||
list: list[T]{locker: locker.EmptyLocker},
|
||||
l :=
|
||||
&LinkedList[T]{
|
||||
list: list[T]{locker: locker.EmptyLocker},
|
||||
}
|
||||
|
||||
for _, e := range elems {
|
||||
l.pushBackNode(&LinkedNode[T]{Value: e})
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *LinkedList[T]) PushBack(v T) *LinkedList[T] {
|
||||
@ -62,6 +69,20 @@ func (l *LinkedList[T]) ForEach(fn func(T) bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LinkedList[T]) GetAt(i int) T {
|
||||
if i <= l.Size() {
|
||||
var n int
|
||||
for current := l.front; current != nil; current = current.Next {
|
||||
if n == i {
|
||||
return current.Value
|
||||
}
|
||||
n++
|
||||
}
|
||||
}
|
||||
|
||||
return *new(T)
|
||||
}
|
||||
|
||||
func (l *LinkedList[T]) Remove(n *LinkedNode[T]) {
|
||||
l.locker.Lock()
|
||||
defer l.locker.Unlock()
|
||||
|
@ -85,3 +85,14 @@ func BenchmarkLinkedList(b *testing.B) {
|
||||
l.PushBack(i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveNode(t *testing.T) {
|
||||
l := list.NewLinkedList(1, 2, 4)
|
||||
|
||||
l.ForEach(func(i int) bool {
|
||||
t.Log(i)
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user