github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/cols/linklist/node.go (about) 1 package linklist 2 3 // Node is an element of a linked list. 4 // It should not be created directly. 5 type Node[T any] struct { 6 Value T // The value stored in the node. 7 8 list *List[T] 9 10 prev, next *Node[T] 11 } 12 13 // Prev returns the previous node in the List. 14 func (node *Node[T]) Prev() *Node[T] { 15 return node.prev 16 } 17 18 // Next returns the next node in the List. 19 func (node *Node[T]) Next() *Node[T] { 20 return node.next 21 } 22 23 // InsertBefore inserts the specified element immediately before this node. 24 func (node *Node[T]) InsertBefore(value T) { 25 newNode := &Node[T]{value, node.list, node.prev, node} 26 27 if node.prev != nil { 28 node.prev.next = newNode 29 } else { 30 node.list.head = newNode 31 } 32 33 node.prev = newNode 34 node.list.size++ 35 } 36 37 // InsertAfter inserts the specified element immediately after this node. 38 func (node *Node[T]) InsertAfter(value T) { 39 newNode := &Node[T]{value, node.list, node, node.next} 40 41 if node.next != nil { 42 node.next.prev = newNode 43 } else { 44 node.list.tail = newNode 45 } 46 47 node.next = newNode 48 node.list.size++ 49 }