github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/list/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  // NodeElement is the basic interface for node element.
     8  // Alignment of interface is 8 bytes and size of interface is 16 bytes.
     9  type NodeElement[T comparable] interface {
    10  	HasNext() bool
    11  	GetNext() NodeElement[T]
    12  	HasPrev() bool
    13  	GetPrev() NodeElement[T]
    14  	GetValue() T
    15  	SetValue(v T) // Concurrent data race error
    16  }
    17  
    18  // BasicLinkedList is the basic interface for linked list. Serve as the singly linked list.
    19  type BasicLinkedList[T comparable] interface {
    20  	Len() int64
    21  	// Append appends the elements to the list l and returns the new elements.
    22  	Append(elements ...NodeElement[T]) []NodeElement[T]
    23  	// AppendValue appends the values to the list l and returns the new elements.
    24  	AppendValue(values ...T) []NodeElement[T]
    25  	// InsertAfter inserts a value v as a new element immediately after element dstE and returns new element.
    26  	// If e is nil, the value v will not be inserted.
    27  	InsertAfter(v T, dstE NodeElement[T]) NodeElement[T]
    28  	// InsertBefore inserts a value v as a new element immediately before element dstE and returns new element.
    29  	// If e is nil, the value v will not be inserted.
    30  	InsertBefore(v T, dstE NodeElement[T]) NodeElement[T]
    31  	// Remove removes targetE from l if targetE is an element of list l and returns targetE or nil if list is empty.
    32  	Remove(targetE NodeElement[T]) NodeElement[T]
    33  	// ForEach traverses the list l and executes function fn for each element.
    34  	ForEach(fn func(idx int64, e NodeElement[T]))
    35  	// FindFirst finds the first element that satisfies the compareFn and returns the element and true if found.
    36  	// If compareFn is not provided, it will use the default compare function that compares the value of element.
    37  	FindFirst(v T, compareFn ...func(e NodeElement[T]) bool) (NodeElement[T], bool)
    38  }
    39  
    40  // LinkedList is the doubly linked list interface.
    41  type LinkedList[T comparable] interface {
    42  	BasicLinkedList[T]
    43  	// ReverseForEach iterates the list in reverse order, calling fn for each element,
    44  	// until either all elements have been visited.
    45  	ReverseForEach(fn func(idx int64, e NodeElement[T]))
    46  	// Front returns the first element of doubly linked list l or nil if the list is empty.
    47  	Front() NodeElement[T]
    48  	// Back returns the last element of doubly linked list l or nil if the list is empty.
    49  	Back() NodeElement[T]
    50  	// PushFront inserts a new element e with value v at the front of list l and returns e.
    51  	PushFront(v T) NodeElement[T]
    52  	// PushBack inserts a new element e with value v at the back of list l and returns e.
    53  	PushBack(v T) NodeElement[T]
    54  	// MoveToFront moves element e to the front of list l.
    55  	MoveToFront(targetE NodeElement[T])
    56  	// MoveToBack moves element e to the back of list l.
    57  	MoveToBack(targetE NodeElement[T])
    58  	// MoveBefore moves element srcE in front of element dstE.
    59  	MoveBefore(srcE, dstE NodeElement[T])
    60  	// MoveAfter moves element srcE next to element dstE.
    61  	MoveAfter(srcE, dstE NodeElement[T])
    62  	// PushFrontList inserts a copy of another linked list at the front of list l.
    63  	PushFrontList(srcList LinkedList[T])
    64  	// PushBackList inserts a copy of another linked list at the back of list l.
    65  	PushBackList(srcList LinkedList[T])
    66  }