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

     1  package list
     2  
     3  // Note that the singly linked list is not thread safe.
     4  // And the singly linked list could be implemented by using the doubly linked list.
     5  // So it is a meaningless exercise to implement the singly linked list.
     6  
     7  // BasicLinkedList is a singly linked list interface.
     8  type BasicLinkedList[T comparable] interface {
     9  	Len() int64
    10  	// Append appends the elements to the list l and returns the new elements.
    11  	Append(elements ...*NodeElement[T]) []*NodeElement[T]
    12  	// AppendValue appends the values to the list l and returns the new elements.
    13  	AppendValue(values ...T) []*NodeElement[T]
    14  	// InsertAfter inserts a value v as a new element immediately after element dstE and returns new element.
    15  	// If e is nil, the value v will not be inserted.
    16  	InsertAfter(v T, dstE *NodeElement[T]) *NodeElement[T]
    17  	// InsertBefore inserts a value v as a new element immediately before element dstE and returns new element.
    18  	// If e is nil, the value v will not be inserted.
    19  	InsertBefore(v T, dstE *NodeElement[T]) *NodeElement[T]
    20  	// Remove removes targetE from l if targetE is an element of list l and returns targetE or nil if the list is empty.
    21  	Remove(targetE *NodeElement[T]) *NodeElement[T]
    22  	// Foreach traverses the list l and executes function fn for each element.
    23  	// If fn returns an error, the traversal stops and returns the error.
    24  	Foreach(fn func(idx int64, e *NodeElement[T]) error) error
    25  	// FindFirst finds the first element that satisfies the compareFn and returns the element and true if found.
    26  	// If compareFn is not provided, it will use the default compare function that compares the value of element.
    27  	FindFirst(v T, compareFn ...func(e *NodeElement[T]) bool) (*NodeElement[T], bool)
    28  }
    29  
    30  // LinkedList is the doubly linked list interface.
    31  type LinkedList[T comparable] interface {
    32  	BasicLinkedList[T]
    33  	// ReverseForeach iterates the list in reverse order, calling fn for each element,
    34  	// until either all elements have been visited.
    35  	ReverseForeach(fn func(idx int64, e *NodeElement[T]))
    36  	// Front returns the first element of doubly linked list l or nil if the list is empty.
    37  	Front() *NodeElement[T]
    38  	// Back returns the last element of doubly linked list l or nil if the list is empty.
    39  	Back() *NodeElement[T]
    40  	// PushFront inserts a new element e with value v at the front of list l and returns e.
    41  	PushFront(v T) *NodeElement[T]
    42  	// PushBack inserts a new element e with value v at the back of list l and returns e.
    43  	PushBack(v T) *NodeElement[T]
    44  	// MoveToFront moves an element e to the front of list l.
    45  	MoveToFront(targetE *NodeElement[T]) bool
    46  	// MoveToBack moves an element e to the back of list l.
    47  	MoveToBack(targetE *NodeElement[T]) bool
    48  	// MoveBefore moves an element srcE in front of element dstE.
    49  	MoveBefore(srcE, dstE *NodeElement[T]) bool
    50  	// MoveAfter moves an element srcE next to element dstE.
    51  	MoveAfter(srcE, dstE *NodeElement[T]) bool
    52  	// PushFrontList inserts a copy of another linked list at the front of list l.
    53  	PushFrontList(srcList LinkedList[T])
    54  	// PushBackList inserts a copy of another linked list at the back of list l.
    55  	PushBackList(srcList LinkedList[T])
    56  }