github.com/avicd/go-utilx@v0.1.0/datax/linked.go (about) 1 package datax 2 3 type LinkedNode[T any] struct { 4 Item T 5 Pre *LinkedNode[T] 6 Next *LinkedNode[T] 7 } 8 9 func (it *LinkedNode[T]) Append(item T) *LinkedNode[T] { 10 next := &LinkedNode[T]{ 11 Item: item, 12 Pre: it, 13 } 14 if it != nil { 15 it.Next = next 16 } 17 return next 18 } 19 20 func (it *LinkedNode[T]) Remove() { 21 if it == nil { 22 return 23 } 24 if it.Pre != nil { 25 it.Pre.Next = it.Next 26 } 27 if it.Next != nil { 28 it.Next.Pre = it.Pre 29 } 30 it.Next = nil 31 it.Pre = nil 32 } 33 34 func (it *LinkedNode[T]) MoveTo(ref *LinkedNode[T]) { 35 if it == ref || it == nil || ref == nil { 36 return 37 } 38 it.Remove() 39 ref.Insert(it) 40 } 41 42 func (it *LinkedNode[T]) Insert(node *LinkedNode[T]) { 43 if it == nil || node == nil { 44 return 45 } 46 if it.Pre != nil { 47 it.Pre.Next = node 48 } 49 node.Pre = it.Pre 50 it.Pre = node 51 node.Next = it 52 } 53 54 func (it *LinkedNode[T]) InsertAfter(node *LinkedNode[T]) { 55 if it == nil || node == nil { 56 return 57 } 58 if it.Next == nil { 59 it.Next = node 60 node.Pre = it 61 } else { 62 it.Next.Insert(node) 63 } 64 }