// Element 表示列表中的一个节点 type Element struct { // Next and previous pointers in the doubly-linked list of elements. // To simplify the implementation, internally a list l is implemented // as a ring, such that &l.root is both the next element of the last // list element (l.Back()) and the previous element of the first list // element (l.Front()).
// 指向下一个节点和前一个节点的指针 next, prev *Element
// The list to which this element belongs. // List指针表示该节点所属的列表 list *List
// The value stored with this element. // 节点的值 Value any }
// Next 返回下一个节点口的指针 func(e *Element)Next() *Element { // 如果e.list != nil 且下一个节点不是root则返回e.next if p := e.next; e.list != nil && p != &e.list.root { return p } returnnil }
// Prev 返回上一个节点的指针 func(e *Element)Prev() *Element { // 如果e.list != nil 且下一个节点不是root则返回e.prev if p := e.prev; e.list != nil && p != &e.list.root { return p } returnnil }
// List 表示一个双向链表 // 链表的最后一个节点的next指针和链表的第一个节点的prev指针都指向root type List struct { root Element // sentinel list element, only &root, root.prev, and root.next are used lenint// current list length excluding (this) sentinel element }
// move 将节点e移动到节点at之后 func(l *List)move(e, at *Element) { if e == at { return } e.prev.next = e.next e.next.prev = e.prev
e.prev = at e.next = at.next e.prev.next = e e.next.prev = e }
// Remove 将节点从列表中移除 func(l *List)Remove(e *Element)any { if e.list == l { // if e.list == l, l must have been initialized when e was inserted // in l or l == nil (e is a zero Element) and l.remove will crash l.remove(e) } return e.Value }
// InsertBefore 将v插入到节点mark之前 func(l *List)InsertBefore(v any, mark *Element) *Element { if mark.list != l { returnnil } // see comment in List.Remove about initialization of l return l.insertValue(v, mark.prev) }
// InsertAfter 将v插入到节点mark之后 func(l *List)InsertAfter(v any, mark *Element) *Element { if mark.list != l { returnnil } // see comment in List.Remove about initialization of l return l.insertValue(v, mark) }
// MoveToFront 将节点e移动到列表头 func(l *List)MoveToFront(e *Element) { if e.list != l || l.root.next == e { return } // see comment in List.Remove about initialization of l l.move(e, &l.root) }
// MoveToBack 将节点e移动到列表末尾 func(l *List)MoveToBack(e *Element) { if e.list != l || l.root.prev == e { return } // see comment in List.Remove about initialization of l l.move(e, l.root.prev) }
// MoveBefore 将节点e移动到mark之前 func(l *List)MoveBefore(e, mark *Element) { if e.list != l || e == mark || mark.list != l { return } l.move(e, mark.prev) }
// MoveAfter 将节点e移动到mark之后 func(l *List)MoveAfter(e, mark *Element) { if e.list != l || e == mark || mark.list != l { return } l.move(e, mark) }
// PushBackList 将一个列表中的所有元素加入l中,在l之后 func(l *List)PushBackList(other *List) { l.lazyInit() for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { l.insertValue(e.Value, l.root.prev) } }
// PushFrontList 将一个列表中的所有元素加入l中,在l之前 func(l *List)PushFrontList(other *List) { l.lazyInit() for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { l.insertValue(e.Value, &l.root) } }