Go:标准库-Container包

参考:知乎
参考:Go语言标准库
参考:Go标准库

1、list - 双向链表

参考:list详解
参考: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{}))
发布了117 篇原创文章 · 获赞 15 · 访问量 5622

猜你喜欢

转载自blog.csdn.net/qq_34326321/article/details/104793316