文章目錄
1、list - 雙向鏈表
1、初始化list
// New returns an initialized list.
func New() *List
2、返回元素,取值
3、清空所有元素
這個方法是初始化方法,同時也會清空原來list中的所有元素
// Init initializes or clears list l.
func (l *List) Init() *List
4、插入元素
1、在列表頭部插入元素/列表元素
//插入對應的元素
// PushFront inserts a new element e with value v at the front of list l and returns e.
func (l *List) PushFront(v interface{})
//插入對應的元素列表
// PushFrontList inserts a copy of an other list at the front of list l.
// The lists l and other may be the same. They must not be nil.
func (l *List) PushFrontList
2、在列表尾部插入元素/列表元素
//插入元素
// 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
//插入元素列表
// PushBackList inserts a copy of an other list at the back of list l.
// The lists l and other may be the same. They must not be nil.
func (l *List) PushBackList(other *List)
3、插入在對應元素之後
出參爲插入的元素
// InsertAfter inserts a new element e with value v immediately after mark and returns e.
// If mark is not an element of l, the list is not modified.
// The mark must not be nil.
func (l *List) InsertAfter(v interface{}, mark *Element) *Element
4、插入在對應元素之前
出參爲插入的元素
// InsertBefore inserts a new element e with value v immediately before mark and returns e.
// If mark is not an element of l, the list is not modified.
// The mark must not be nil.
func (l *List) InsertBefore(v interface{}, mark *Element) *Element
5、list長度
// Len returns the number of elements of list l.
// The complexity is O(1).
func (l *List) Len() int { return l.len }
6、移動元素
1、移動到list第一個
// MoveToFront moves element e to the front of list l.
// If e is not an element of l, the list is not modified.
// The element must not be nil.
func (l *List) MoveToFront(e *Element)
2、移動到list最後一個
// MoveToBack moves element e to the back of list l.
// If e is not an element of l, the list is not modified.
// The element must not be nil.
func (l *List) MoveToBack(e *Element)
3、移動到某個元素之前
// MoveBefore moves element e to its new position before mark.
// If e or mark is not an element of l, or e == mark, the list is not modified.
// The element and mark must not be nil.
func (l *List) MoveBefore(e, mark *Element)
4、移動到某個元素之後
// MoveAfter moves element e to its new position after mark.
// If e or mark is not an element of l, or e == mark, the list is not modified.
// The element and mark must not be nil.
func (l *List) MoveAfter(e, mark *Element)
7、移除元素
// Remove removes e from l if e is an element of list l.
// It returns the element value e.Value.
// The element must not be nil.
func (l *List) Remove(e *Element) interface{}
2、heap - 堆
1、結構說明
根據go這邊的定義,如果一個類型實現對應的heap兩個方法和sort的三個方法即可實現一個自定義的堆類型。
type Interface interface {
sort.Interface
Push(x interface{}) // add x as element Len()
Pop() interface{} // remove and return element Len() - 1.
}
2、初始化堆
初始化需要傳入自定義實現的堆類型(實現接口五個方法)
// Init establishes the heap invariants required by the other routines in this package.
// Init is idempotent with respect to the heap invariants
// and may be called whenever the heap invariants may have been invalidated.
// The complexity is O(n) where n = h.Len().
func Init(h Interface)
2、元素出堆(刪除)
根據less的實現方法,調出最小的元素,返回該元素,在堆中,該元素被刪除
func Pop(h Interface) interface{}
3、壓入元素
/向堆h中插入元素x,並保持堆的約束性。複雜度O(log(n)),其中n等於h.Len()。
func Push(h Interface, x interface{})
4、刪除第i個元素
//刪除堆中的第i個元素,並保持堆的約束性。複雜度O(log(n)),其中n等於h.Len()。
func Remove(h Interface, i int) interface{}
5、堆修復
//在修改第i個元素後,調用本函數修復堆,比刪除第i個元素後插入新元素更有效率。
//複雜度O(log(n)),其中n等於h.Len()。
func Fix(h Interface, i int)
3、ring - 環
參考:Go:環
類似雙向循環鏈表
.
// A Ring is an element of a circular list, or ring.
// Rings do not have a beginning or end; a pointer to any ring element
// serves as reference to the entire ring. Empty rings are represented
// as nil Ring pointers. The zero value for a Ring is a one-element
// ring with a nil Value.
//
1、初始化/創建
1、初始化環
func (r *Ring) init() *Ring
2、創建一個n個元素的環
// New creates a ring of n elements.
func New(n int) *Ring
2、獲取前後元素
1、返回前一個元素
// Prev returns the previous ring element. r must not be empty.
func (r *Ring) Prev() *Ring
2、返回下一個元素
// Next returns the next ring element. r must not be empty.
func (r *Ring) Next() *Ring
3、移動
返回r移動n%r.Len()個位置(n>=0向前移動,n<0向後移動)後的元素,r不能爲空。
// Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0)
// in the ring and returns that ring element. r must not be empty.
func (r *Ring) Move(n int) *Ring
4、連接與斷開連接
1、連接環
根據註釋
如果r和s指向同一個環形鏈表,則會刪除掉r和s之間的元素,刪掉的元素構成一個子鏈表,返回指向該子鏈表的指針(r的原後繼元素);如果沒有刪除元素,則仍然返回r的原後繼元素,而不是nil。
如果r和s指向不同的鏈表,將創建一個單獨的鏈表,將s指向的鏈表插入r後面,返回s原最後一個元素後面的元素(即r的原後繼元素)。
func (r *Ring) Link(s *Ring) *Ring
2、斷開環
根據註釋
刪除鏈表中n % r.Len()個元素,從r.Next()開始刪除。如果n % r.Len() == 0,不修改r。返回刪除的元素構成的鏈表,r不能爲空。
func (r *Ring) Unlink(n int) *Ring
5、長度
// Len computes the number of elements in ring r.
// It executes in time proportional to the number of elements.
//
func (r *Ring) Len() int
6、對環的每個元素執行func
傳入一個函數,對環中的所有元素都執行一遍這個函數,執行的順序是正向的順序。
如果函數f改變* r,那麼Do的行爲是不確定的。
// Do calls function f on each element of the ring, in forward order.
// The behavior of Do is undefined if f changes *r.
func (r *Ring) Do(f func(interface{}))