1.结点Element
1.1 结点结构
// Element is an element of a linked list.
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
// The value stored with this element.
Value interface{}
}
包括前向指针prev
和后向指针next
。
同时还有一个指向链表的指针list
。
1.2 结点相关函数
1.2.1 获取下一个结点
// Next returns the next list element or nil.
func (e *Element) Next() *Element {
if p := e.next; e.list != nil && p != &e.list.root {
return p
}
return nil
}
这里需要理解判断条件:e.list != nil && p != &e.list.root
e.list != nil
是说结点指向的链表非空;
p != &e.list.root
是说当前结点的下一个结点不是链表的root
结点。
go中链表的整个结构如下图所示:
尾结点的next
是root
,所以说如果一个结点的next
是root
的话,那么说明这个结点是链表的最后一个结点,那么他的next
应该为nil
.
1.2.2 获取前一个结点
// Prev returns the previous list element or nil.
func (e *Element) Prev() *Element {
if p := e.prev; e.list != nil && p != &e.list.root {
return p
}
return nil
}
同样需要判断链表是不是空,以及前一个结点是不是root
结点,如果前一个结点是root
结点,说明该结点是链表的头结点,头结点的prev
应该为nil
。
2. list
2.1 list的结构
type List struct {
root Element // sentinel list element, only &root, root.prev, and root.next are used
len int // current list length excluding (this) sentinel element
}
链表的结构包括一个root结点和一个长度。
2.2 链表相关的函数
2.2.1 Init 初始化一个链表
// Init initializes or clears list l.
func (l *List) Init() *List {
l.root.next = &l.root
l.root.prev = &l.root
l.len = 0
return l
}
l.root.next = &l.root
l.root.prev = &l.root
root结点自己形成一个环。
New()函数:
// New returns an initialized list.
func New() *List { return new(List).Init() }
2.2.2 Len 获取链表长度
func (l *List) Len() int { return l.len }
2.2.3 Front/Back 获取头/尾结点
Front()
获取头结点;
Back()
获取尾结点;
// Front returns the first element of list l or nil if the list is empty.
func (l *List) Front() *Element {
if l.len == 0 {
return nil
}
return l.root.next
}
// Back returns the last element of list l or nil if the list is empty.
func (l *List) Back() *Element {
if l.len == 0 {
return nil
}
return l.root.prev
}
2.2.4 insert 插入结点
在结点at之后插入e结点,并将e结点返回。
// insert inserts e after at, increments l.len, and returns e.
func (l *List) insert(e, at *Element) *Element {
n := at.next
at.next = e
e.prev = at
e.next = n
n.prev = e
e.list = l
l.len++
return e
}
根据值插入结点:如果只有值,没有结点,使用insertValue
函数,通过值创建一个结点然后将结点插入。
// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
func (l *List) insertValue(v interface{}, at *Element) *Element {
return l.insert(&Element{Value: v}, at)
}
2.2.5 remove/Remove 删除一个结点
从链表中删除结点e,并将删除的结点返回。该函数的函数名是小写的。
// remove removes e from its list, decrements l.len, and returns e.
func (l *List) remove(e *Element) *Element {
e.prev.next = e.next
e.next.prev = e.prev
e.next = nil // avoid memory leaks
e.prev = nil // avoid memory leaks
e.list = nil
l.len--
return e
}
双向链表的删除,将删除结点的next prev list置为nil。
来一个大写的Remove:
func (l *List) Remove(e *Element) interface{} {
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
}
从list中删除结点e,如果e不是list中的结点,则直接返回e的value。
2.2.6 move 移动结点
将结点e移动到结点at之后,包含删除和插入两个操作。
// move moves e to next to at and returns e.
func (l *List) move(e, at *Element) *Element {
if e == at {
return e
}
e.prev.next = e.next
e.next.prev = e.prev
n := at.next
at.next = e
e.prev = at
e.next = n
n.prev = e
return e
}
在此基础上,衍生出几个函数:
函数名 | 功能 |
---|---|
MoveToFront | 将结点移动至链表头部 |
MoveToBack | 将结点移动至链表尾部 |
MoveBefore | 将结点移动至结点mark前 |
MoveAfter | 将结点移动至结点mark后 |
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)
}
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)
}
func (l *List) MoveBefore(e, mark *Element) {
if e.list != l || e == mark || mark.list != l {
return
}
l.move(e, mark.prev)
}
func (l *List) MoveAfter(e, mark *Element) {
if e.list != l || e == mark || mark.list != l {
return
}
l.move(e, mark)
}
2.2.7 PushFront/PushBack 链表头尾插入值
PushFront在链表头部插入一个值为v的结点;
PushBack在链表尾部插入一个值为v的结点;
// PushFront inserts a new element e with value v at the front of list l and returns e.
func (l *List) PushFront(v interface{}) *Element {
l.lazyInit()
return l.insertValue(v, &l.root)
}
// PushBack inserts a new element e with value v at the back of list l and returns e.
func (l *List) PushBack(v interface{}) *Element {
l.lazyInit()
return l.insertValue(v, l.root.prev)
}
2.2.8 InsertBefore/InsertAfter 某个结点前/后插入值
直接调用内部的函数。
func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
if mark.list != l {
return nil
}
return l.insertValue(v, mark.prev)
}
func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
if mark.list != l {
return nil
}
return l.insertValue(v, mark)
}
2.2.9 PushBackList/PushFrontList合并链表
1.func (l *List) PushBackList(other *List)
将链表other
插入到链表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)
}
}
遍历other
链表,然后调用insertValue
将每个结点插入到l
尾部。
2.func (l *List) PushFrontList(other *List)
将链表other
插入到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)
}
}
此时要注意,这里other
遍历的方向是从后往前。
每次在l.root
后面插入结点。