go中的list

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中鏈表的整個結構如下圖所示:
在這裏插入圖片描述
尾結點的nextroot,所以說如果一個結點的nextroot的話,那麼說明這個結點是鏈表的最後一個結點,那麼他的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後面插入結點。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章