github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/list/linked_list_node.go (about)

     1  package list
     2  
     3  type NodeElement[T comparable] struct {
     4  	prev, next *NodeElement[T]
     5  	listRef    *doublyLinkedList[T]
     6  	Value      T // The type of value may be a small size type.
     7  	// It should be placed at the end of the struct to avoid taking too much padding.
     8  }
     9  
    10  func NewNodeElement[T comparable](v T) *NodeElement[T] {
    11  	return newNodeElement[T](v, nil)
    12  }
    13  
    14  func newNodeElement[T comparable](v T, list *doublyLinkedList[T]) *NodeElement[T] {
    15  	return &NodeElement[T]{
    16  		Value:   v,
    17  		listRef: list,
    18  	}
    19  }
    20  
    21  func (e *NodeElement[T]) HasNext() bool {
    22  	if e == nil {
    23  		return false
    24  	}
    25  	return e.next != nil && e.next != e.listRef.getRoot()
    26  }
    27  
    28  func (e *NodeElement[T]) HasPrev() bool {
    29  	if e == nil {
    30  		return false
    31  	}
    32  	return e.prev != nil && e.prev != e.listRef.getRoot()
    33  }
    34  
    35  func (e *NodeElement[T]) Next() *NodeElement[T] {
    36  	if e == nil {
    37  		return nil
    38  	}
    39  	return e.next
    40  }
    41  
    42  func (e *NodeElement[T]) Prev() *NodeElement[T] {
    43  	if e == nil {
    44  		return nil
    45  	}
    46  	return e.prev
    47  }