github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/container/list/list.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package list implements a doubly linked list.
     6  //
     7  // To iterate over a list (where l is a *List):
     8  //	for e := l.Front(); e != nil; e = e.Next() {
     9  //		// do something with e.Value
    10  //	}
    11  //
    12  // list 实现了一个双链表
    13  /*
    14  链表迭代
    15  for e := l.Front(); e !=nil e = e.Next(){
    16  	// do something with e.Value
    17  }
    18  */
    19  package list
    20  
    21  // Element is an element of a linked list.
    22  // 一个链表元素结构
    23  type Element struct {
    24  	// Next and previous pointers in the doubly-linked list of elements.
    25  	// To simplify the implementation, internally a list l is implemented
    26  	// as a ring, such that &l.root is both the next element of the last
    27  	// list element (l.Back()) and the previous element of the first list
    28  	// element (l.Front()).
    29  	next, prev *Element // 上一个和下一个元素的指针
    30  
    31  	// The list to which this element belongs.
    32  	list *List // 元素所在的链表
    33  
    34  	// The value stored with this element.
    35  	Value interface{} // 元素值
    36  }
    37  
    38  // Next returns the next list element or nil.
    39  // 返回该元素的下一个元素,如果没有下一个元素则返回 nil
    40  // 怎么判断没有下一个元素
    41  // 首先要判断当前链表存在, e.list !=nil
    42  // 其次要判断取出的链表节点 不等于根节点, p != &e.list.root
    43  // 因为初始化链表时 根节点的首位都是赋值给root 根节点了
    44  func (e *Element) Next() *Element {
    45  	if p := e.next; e.list != nil && p != &e.list.root {
    46  		return p
    47  	}
    48  	return nil
    49  }
    50  
    51  // Prev returns the previous list element or nil.
    52  // 返回该元素的前一个元素,如果没有前一个元素则返回nil
    53  func (e *Element) Prev() *Element {
    54  	if p := e.prev; e.list != nil && p != &e.list.root {
    55  		return p
    56  	}
    57  	return nil
    58  }
    59  
    60  // List represents a doubly linked list.
    61  // The zero value for List is an empty list ready to use.
    62  // List 是一个双链表
    63  type List struct {
    64  	// root 链表的根元素, 用来判断链表是否已经到了链尾了
    65  	root Element // sentinel list element, only &root, root.prev, and root.next are used
    66  	// 链表长度
    67  	len int // current list length excluding (this) sentinel element
    68  }
    69  
    70  // Init initializes or clears list l.
    71  // 初始化或者清空一个链表
    72  // 初始化一个链表时, root.next 和 root.prev 都等于root
    73  // 当root.next == root.prev时 表示链表已经到链尾了
    74  func (l *List) Init() *List {
    75  	l.root.next = &l.root
    76  	l.root.prev = &l.root
    77  	l.len = 0
    78  	return l
    79  }
    80  
    81  // New returns an initialized list.
    82  // 返回一个初始化的list
    83  func New() *List { return new(List).Init() }
    84  
    85  // Len returns the number of elements of list l.
    86  // The complexity is O(1).
    87  // 获取 list l 的长度
    88  func (l *List) Len() int { return l.len }
    89  
    90  // Front returns the first element of list l or nil if the list is empty.
    91  // 返回链表中的第一个元素,如果为空, 就是链表长度 l.len == 0  则返回nil
    92  func (l *List) Front() *Element {
    93  	if l.len == 0 {
    94  		return nil
    95  	}
    96  	return l.root.next
    97  }
    98  
    99  // Back returns the last element of list l or nil if the list is empty.
   100  // 返回最后一个元素, 如果为空 则返回 nil
   101  func (l *List) Back() *Element {
   102  	if l.len == 0 {
   103  		return nil
   104  	}
   105  	return l.root.prev
   106  }
   107  
   108  // lazyInit lazily initializes a zero List value.
   109  // 延迟初始化
   110  func (l *List) lazyInit() {
   111  	// 如果是空链表,则初始化
   112  	if l.root.next == nil {
   113  		l.Init()
   114  	}
   115  }
   116  
   117  // insert inserts e after at, increments l.len, and returns e.
   118  // 在at 后面插入一个元素e, 并更新链表长度, 并返回e
   119  func (l *List) insert(e, at *Element) *Element {
   120  	e.prev = at
   121  	e.next = at.next
   122  	e.prev.next = e
   123  	e.next.prev = e
   124  	e.list = l
   125  	l.len++
   126  	return e
   127  }
   128  
   129  // insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
   130  func (l *List) insertValue(v interface{}, at *Element) *Element {
   131  	return l.insert(&Element{Value: v}, at)
   132  }
   133  
   134  // remove removes e from its list, decrements l.len, and returns e.
   135  // 移除元素e
   136  // 这里默认l 不是空链表
   137  func (l *List) remove(e *Element) *Element {
   138  	e.prev.next = e.next
   139  	e.next.prev = e.prev
   140  	e.next = nil // avoid memory leaks
   141  	e.prev = nil // avoid memory leaks
   142  	e.list = nil
   143  	l.len--
   144  	return e
   145  }
   146  
   147  // move moves e to next to at and returns e.
   148  // 将e 移动到at 后面
   149  func (l *List) move(e, at *Element) *Element {
   150  	// 如果e 就是at 就不操作
   151  	if e == at {
   152  		return e
   153  	}
   154  	// 将e 剥离出来
   155  	e.prev.next = e.next
   156  	e.next.prev = e.prev
   157  
   158  	// 挂载到at 后面
   159  	e.prev = at
   160  	e.next = at.next
   161  	e.prev.next = e
   162  	e.next.prev = e
   163  
   164  	return e
   165  }
   166  
   167  // Remove removes e from l if e is an element of list l.
   168  // It returns the element value e.Value.
   169  // The element must not be nil.
   170  // 在链表l 中有e 元素 则移除它, 不管是否移除成功, 都返回e元素的值
   171  func (l *List) Remove(e *Element) interface{} {
   172  	if e.list == l { // 只有e元素在链表中才移除
   173  		// if e.list == l, l must have been initialized when e was inserted
   174  		// in l or l == nil (e is a zero Element) and l.remove will crash
   175  		// 如果e 是一个空元素 而且 链表也是空的, 则会crash掉
   176  		l.remove(e)
   177  	}
   178  	return e.Value
   179  }
   180  
   181  // PushFront inserts a new element e with value v at the front of list l and returns e.
   182  // 在 list l 的首部插入值为 v 的元素,并返回该元素
   183  func (l *List) PushFront(v interface{}) *Element {
   184  	l.lazyInit() // 判断如果为空链表则先初始化
   185  	return l.insertValue(v, &l.root)
   186  }
   187  
   188  // PushBack inserts a new element e with value v at the back of list l and returns e.
   189  // 在 list l 的末尾插入值为 v 的元素,并返回该元素
   190  func (l *List) PushBack(v interface{}) *Element {
   191  	l.lazyInit()
   192  	return l.insertValue(v, l.root.prev)
   193  }
   194  
   195  // InsertBefore inserts a new element e with value v immediately before mark and returns e.
   196  // If mark is not an element of l, the list is not modified.
   197  // The mark must not be nil.
   198  // 在 list l 中元素 mark 之前插入一个值为 v 的元素,并返回该元素,如果 mark 不是list中元素,则 list 不改变
   199  func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
   200  	if mark.list != l {
   201  		return nil
   202  	}
   203  	// see comment in List.Remove about initialization of l
   204  	return l.insertValue(v, mark.prev)
   205  }
   206  
   207  // InsertAfter inserts a new element e with value v immediately after mark and returns e.
   208  // If mark is not an element of l, the list is not modified.
   209  // The mark must not be nil.
   210  // 在mark 元素之后 插入一个值为v的元素, 如果mark 不是list中的元素, 则List不改变
   211  func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
   212  	if mark.list != l {
   213  		return nil
   214  	}
   215  	// see comment in List.Remove about initialization of l
   216  	return l.insertValue(v, mark)
   217  }
   218  
   219  // MoveToFront moves element e to the front of list l.
   220  // If e is not an element of l, the list is not modified.
   221  // The element must not be nil.
   222  // 将元素 e 移动到 list l 的首部,如果 e 不属于list l,则list不改变
   223  func (l *List) MoveToFront(e *Element) {
   224  	if e.list != l || l.root.next == e {
   225  		return
   226  	}
   227  	// see comment in List.Remove about initialization of l
   228  	l.move(e, &l.root)
   229  }
   230  
   231  // MoveToBack moves element e to the back of list l.
   232  // If e is not an element of l, the list is not modified.
   233  // The element must not be nil.
   234  // 将元素 e 移动到 list l 的末尾,如果 e 不属于list l,则list不改变
   235  func (l *List) MoveToBack(e *Element) {
   236  	if e.list != l || l.root.prev == e {
   237  		return
   238  	}
   239  	// see comment in List.Remove about initialization of l
   240  	l.move(e, l.root.prev)
   241  }
   242  
   243  // MoveBefore moves element e to its new position before mark.
   244  // If e or mark is not an element of l, or e == mark, the list is not modified.
   245  // The element and mark must not be nil.
   246  // 将元素 e 移动到元素 mark 之前,如果元素e 或者 mark 不属于 list l,或者 e==mark,则 list l 不改变
   247  func (l *List) MoveBefore(e, mark *Element) {
   248  	if e.list != l || e == mark || mark.list != l {
   249  		return
   250  	}
   251  	l.move(e, mark.prev)
   252  }
   253  
   254  // MoveAfter moves element e to its new position after mark.
   255  // If e or mark is not an element of l, or e == mark, the list is not modified.
   256  // The element and mark must not be nil.
   257  // 将元素 e 移动到元素 mark 之后,如果元素e 或者 mark 不属于 list l,或者 e==mark,则 list l 不改变
   258  func (l *List) MoveAfter(e, mark *Element) {
   259  	if e.list != l || e == mark || mark.list != l {
   260  		return
   261  	}
   262  	l.move(e, mark)
   263  }
   264  
   265  // PushBackList inserts a copy of another list at the back of list l.
   266  // The lists l and other may be the same. They must not be nil.
   267  // 在 list l 的尾部插入另外一个 list,其中l 和 other 可以相等, 但是不能为空
   268  func (l *List) PushBackList(other *List) {
   269  	l.lazyInit()
   270  	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
   271  		l.insertValue(e.Value, l.root.prev)
   272  	}
   273  }
   274  
   275  // PushFrontList inserts a copy of another list at the front of list l.
   276  // The lists l and other may be the same. They must not be nil.
   277  // 在 list l 的首部插入另外一个 list,其中 l 和 other 可以相等
   278  func (l *List) PushFrontList(other *List) {
   279  	l.lazyInit()
   280  	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
   281  		l.insertValue(e.Value, &l.root)
   282  	}
   283  }