1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
This commit is contained in:
2022-11-18 16:56:44 +08:00
parent bd85140a78
commit f3ca69f159
2 changed files with 34 additions and 2 deletions

View File

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

View File

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