激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Golang - Go container包的介紹

Go container包的介紹

2022-01-25 00:39Kevin_cai09 Golang

這篇文章主要介紹了Go container包,go語言container包中有List和Element容器ist和Element都是結(jié)構(gòu)體類型。結(jié)構(gòu)體類型有一個特點,那就是它們的零值都會是擁有其特定結(jié)構(gòu),但沒有任何定制化內(nèi)容的值,相當于一個空殼,下面一起進文章來了解

1.簡介

Container — 容器數(shù)據(jù)類型:該包實現(xiàn)了三個復雜的數(shù)據(jù)結(jié)構(gòu):堆、鏈表、環(huán)

  • List:Go中對鏈表的實現(xiàn),其中List:雙向鏈表,Element:鏈表中的元素
  • Ring:實現(xiàn)的是一個循環(huán)鏈表,也就是我們俗稱的環(huán)
  • Heap:Go中對堆的實現(xiàn)

2.list

簡單實用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
func main()  {
 // 初始化雙向鏈表
 l := list.New()
 // 鏈表頭插入
 l.PushFront(1)
 // 鏈表尾插入
 l.PushBack(2)
 l.PushFront(3)
 // 從頭開始遍歷
 for head := l.Front();head != nil;head = head.Next() {
  fmt.Println(head.Value)
 }
}

方法列表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type Element
    func (e *Element) Next() *Element                                   // 返回該元素的下一個元素,如果沒有下一個元素則返回 nil
    func (e *Element) Prev() *Element                                   // 返回該元素的前一個元素,如果沒有前一個元素則返回nil
 
type List                              
    func New() *List                                                    // 返回一個初始化的list
    func (l *List) Back() *Element                                      // 獲取list l的最后一個元素
    func (l *List) Front() *Element                                     // 獲取list l的第一個元素
    func (l *List) Init() *List                                         // list l 初始化或者清除 list l
    func (l *List) InsertAfter(v interface{}, mark *Element) *Element   // 在 list l 中元素 mark 之后插入一個值為 v 的元素,并返回該元素,如果 mark 不是list中元素,則 list 不改變
    func (l *List) InsertBefore(v interface{}, mark *Element) *Element  // 在 list l 中元素 mark 之前插入一個值為 v 的元素,并返回該元素,如果 mark 不是list中元素,則 list 不改變
    func (l *List) Len() int                                            // 獲取 list l 的長度
    func (l *List) MoveAfter(e, mark *Element)                          // 將元素 e 移動到元素 mark 之后,如果元素e 或者 mark 不屬于 list l,或者 e==mark,則 list l 不改變
    func (l *List) MoveBefore(e, mark *Element)                         // 將元素 e 移動到元素 mark 之前,如果元素e 或者 mark 不屬于 list l,或者 e==mark,則 list l 不改變
    func (l *List) MoveToBack(e *Element)                               // 將元素 e 移動到 list l 的末尾,如果 e 不屬于list l,則list不改變            
    func (l *List) MoveToFront(e *Element)                              // 將元素 e 移動到 list l 的首部,如果 e 不屬于list l,則list不改變            
    func (l *List) PushBack(v interface{}) *Element                     // 在 list l 的末尾插入值為 v 的元素,并返回該元素             
    func (l *List) PushBackList(other *List)                            // 在 list l 的尾部插入另外一個 list,其中l(wèi) 和 other 可以相等              
    func (l *List) PushFront(v interface{}) *Element                    // 在 list l 的首部插入值為 v 的元素,并返回該元素             
    func (l *List) PushFrontList(other *List)                           // 在 list l 的首部插入另外一個 list,其中 l 和 other 可以相等             
    func (l *List) Remove(e *Element) interface{}                       // 如果元素 e 屬于list l,將其從 list 中刪除,并返回元素 e 的值

2.1數(shù)據(jù)結(jié)構(gòu)

節(jié)點定義:

?
1
2
3
4
5
6
7
8
9
10
type Element struct {
 // 后繼指針,前向指針
 next, prev *Element
 
 // 鏈表指針,屬于哪個鏈表
 list *List
 
 // 節(jié)點value
 Value interface{}
}

雙向鏈表定義:

?
1
2
3
4
5
6
type List struct {
  // 根元素
 root Element // sentinel list element, only &root, root.prev, and root.next are used
 // 實際節(jié)點數(shù)量
  len  int     // current list length excluding (this) sentinel element
}

初始化:

?
1
2
3
4
5
6
7
8
9
// 通過工廠方法返回list指針
func New() *List { return new(List).Init() }
 
func (l *List) Init() *List {
 l.root.next = &l.root
 l.root.prev = &l.root
 l.len = 0
 return l
}

這里可以看到root節(jié)點作為一個根節(jié)點,不承擔數(shù)據(jù),也不是實際的鏈表節(jié)點,節(jié)點數(shù)量len沒算上它,再初始化的時候,root節(jié)點會成為一個只有一個節(jié)點的環(huán)(前后指針都指向自己)

2.2插入元素

頭插法:

?
1
2
3
4
5
6
7
8
9
10
11
func (l *List) Front() *Element {
 if l.len == 0 {
  return nil
 }
 return l.root.next
}
 
func (l *List) PushFront(v interface{}) *Element {
 l.lazyInit()
 return l.insertValue(v, &l.root)
}

尾插法:

?
1
2
3
4
5
6
7
8
9
10
11
func (l *List) Back() *Element {
 if l.len == 0 {
  return nil
 }
 return l.root.prev
}
 
func (l *List) PushBack(v interface{}) *Element {
 l.lazyInit()
 return l.insertValue(v, l.root.prev)
}

在指定元素后新增元素:

?
1
2
3
4
5
6
7
8
9
func (l *List) insert(e, at *Element) *Element {
 e.prev = at
 e.next = at.next
 e.prev.next = e
 e.next.prev = e
 e.list = l
 l.len++
 return e
}

這里有個延遲初始化的邏輯:lazyInit,把初始化操作延后,僅在實際需要的時候才進行

?
1
2
3
4
5
func (l *List) lazyInit() {
 if l.root.next == nil {
  l.Init()
 }
}

移除元素:

?
1
2
3
4
5
6
7
8
9
10
// remove 從雙向鏈表中移除一個元素e,遞減鏈表的長度,返回該元素e
func (l *List) remove(e *Element) *Element {
  e.prev.next = e.next
  e.next.prev = e.prev
  e.next = nil // 防止內(nèi)存泄漏
  e.prev = nil // 防止內(nèi)存泄漏
  e.list = nil
  l.len --
  return e
}

3.ring

Go中提供的ring是一個雙向的循環(huán)鏈表,與list的區(qū)別在于沒有表頭和表尾,ring表頭和表尾相連,構(gòu)成一個環(huán)

使用demo:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func main()  {
 
 // 初始化3個元素的環(huán),返回頭節(jié)點
 r := ring.New(3)
 // 給環(huán)填充值
 for i := 1;i <= 3;i++{
  r.Value = i
  r = r.Next()
 }
 sum := 0
 // 對環(huán)的每個元素進行處理
 r.Do(func(i interface{}) {
  sum = i.(int) + sum
 })
 fmt.Println(sum)
}

方法列表:

?
1
2
3
4
5
6
7
8
9
type Ring
    func New(n int) *Ring  // 初始化環(huán)
    func (r *Ring) Do(f func(interface{}))  // 循環(huán)環(huán)進行操作
    func (r *Ring) Len() int // 環(huán)長度
    func (r *Ring) Link(s *Ring) *Ring // 連接兩個環(huán)
    func (r *Ring) Move(n int) *Ring // 指針從當前元素開始向后移動或者向前(n 可以為負數(shù))
    func (r *Ring) Next() *Ring // 當前元素的下個元素
    func (r *Ring) Prev() *Ring // 當前元素的上個元素
    func (r *Ring) Unlink(n int) *Ring // 從當前元素開始,刪除 n 個元素

3.1數(shù)據(jù)結(jié)構(gòu)

環(huán)節(jié)點數(shù)據(jù)結(jié)構(gòu):

?
1
2
3
4
type Ring struct {
 next, prev *Ring // 前繼和后繼指針
 Value      interface{} // for use by client; untouched by this library
}

初始化一個環(huán):后繼和前繼指針都指向自己

?
1
2
3
4
5
func (r *Ring) init() *Ring {
 r.next = r
 r.prev = r
 return r
}

初始化指定數(shù)量個節(jié)點的環(huán)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func New(n int) *Ring {
 if n <= 0 {
  return nil
 }
 r := new(Ring)
 p := r
 for i := 1; i < n; i++ {
  p.next = &Ring{prev: p}
  p = p.next
 }
 p.next = r
 r.prev = p
 return r
}

遍歷環(huán),對個元素執(zhí)行指定操作:

?
1
2
3
4
5
6
7
8
func (r *Ring) Do(f func(interface{})) {
 if r != nil {
  f(r.Value)
  for p := r.Next(); p != r; p = p.next {
   f(p.Value)
  }
 }
}

4.heap

Go中堆使用的數(shù)據(jù)結(jié)構(gòu)是最小二叉樹,即根節(jié)點比左邊子樹和右邊子樹的所有值都小

使用demo:需要實現(xiàn)Interface接口,go中堆都是實現(xiàn)這個接口,定義了排序,插入和刪除方法

?
1
2
3
4
5
type Interface interface {
    sort.Interface
    Push(x interface{}) // add x as element Len()
    Pop() interface{}   // remove and return element Len() - 1.
}

實現(xiàn)接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// An IntHeap is a min-heap of ints.
type IntHeap []int
 
func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
 
func (h *IntHeap) Push(x interface{}) {
 // Push and Pop use pointer receivers because they modify the slice's length,
 // not just its contents.
 *h = append(*h, x.(int))
}
 
func (h *IntHeap) Pop() interface{} {
 old := *h
 n := len(old)
 x := old[n-1]
 *h = old[0 : n-1]
 return x
}
 
// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func Example_intHeap() {
 h := &IntHeap{2, 1, 5}
 heap.Init(h)
 heap.Push(h, 3)
 fmt.Printf("minimum: %d\n", (*h)[0])
 for h.Len() > 0 {
  fmt.Printf("%d ", heap.Pop(h))
 }
 // Output:
 // minimum: 1
 // 1 2 3 5
}

4.1數(shù)據(jù)結(jié)構(gòu)

上浮:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func Push(h Interface, x interface{}) {
 h.Push(x)
 up(h, h.Len()-1)
}
 
func up(h Interface, j int) {
 for {
  i := (j - 1) / 2 // parent
  if i == j || !h.Less(j, i) {
   break
  }
  h.Swap(i, j)
  j = i
 }
}

下沉:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func Pop(h Interface) interface{} {
 n := h.Len() - 1
 h.Swap(0, n)
 down(h, 0, n)
 return h.Pop()
}
 
func down(h Interface, i0, n int) bool {
 i := i0
 for {
  j1 := 2*i + 1
  if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
   break
  }
  j := j1 // left child
  if j2 := j1 + 1; j2 < n && h.Less(j2, j1) {
   j = j2 // = 2*i + 2  // right child
  }
  if !h.Less(j, i) {
   break
  }
  h.Swap(i, j)
  i = j
 }
 return i > i0
}

到此這篇關(guān)于Go container包的介紹的文章就介紹到這了,更多相關(guān)Go container包內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/weixin_41922289/article/details/121592261

延伸 · 閱讀

精彩推薦
  • Golanggolang 通過ssh代理連接mysql的操作

    golang 通過ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過ssh代理連接mysql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    a165861639710342021-03-08
  • Golanggo日志系統(tǒng)logrus顯示文件和行號的操作

    go日志系統(tǒng)logrus顯示文件和行號的操作

    這篇文章主要介紹了go日志系統(tǒng)logrus顯示文件和行號的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    SmallQinYan12302021-02-02
  • Golanggo語言制作端口掃描器

    go語言制作端口掃描器

    本文給大家分享的是使用go語言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
  • Golanggolang的httpserver優(yōu)雅重啟方法詳解

    golang的httpserver優(yōu)雅重啟方法詳解

    這篇文章主要給大家介紹了關(guān)于golang的httpserver優(yōu)雅重啟的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,...

    helight2992020-05-14
  • GolangGolang通脈之數(shù)據(jù)類型詳情

    Golang通脈之數(shù)據(jù)類型詳情

    這篇文章主要介紹了Golang通脈之數(shù)據(jù)類型,在編程語言中標識符就是定義的具有某種意義的詞,比如變量名、常量名、函數(shù)名等等,Go語言中標識符允許由...

    4272021-11-24
  • Golanggolang如何使用struct的tag屬性的詳細介紹

    golang如何使用struct的tag屬性的詳細介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細介紹,從例子說起,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看...

    Go語言中文網(wǎng)11352020-05-21
  • Golanggolang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang中Bit數(shù)組的實現(xiàn)方式

    Golang中Bit數(shù)組的實現(xiàn)方式

    這篇文章主要介紹了Golang中Bit數(shù)組的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    天易獨尊11682021-06-09
主站蜘蛛池模板: 嗯~啊~用力~高h | 久久精品国产99国产精品澳门 | 久色网站 | 91社区电影 | 一级黄色毛片播放 | 色骚综合| 成人性视频免费网站下载软件 | 国产女同疯狂激烈互摸 | 日韩视频―中文字幕 | 激情视频免费看 | 久久69精品久久久久久国产越南 | 免费看性xxx高清视频自由 | 亚洲网站在线观看视频 | 成年免费视频黄网站在线观看 | 免费播放欧美毛片 | 亚洲一区二区三区日本久久九 | 欧美日韩亚洲成人 | 国产精品刺激对白麻豆99 | 色欲香天天天综合网站 | 成人国产精品齐天大性 | 亚洲国产精品久久久久久久久久久 | 久久成人视屏 | 国产免费传媒av片在线 | 日韩欧美电影在线观看 | 一级免费大片 | 免费观看一级欧美大 | 无码专区aaaaaa免费视频 | 欧美a v在线 | 成人福利在线看 | avlululu| 黄色大片在线免费看 | 一级爱爱 | av免费提供 | 视频一区二区国产 | 玩偶姐姐 在线观看 | 欧美性生活网站 | av在线播放电影 | 久草视频免费 | av在线官网 | 成人在线视频精品 | 国产乱淫av片免费观看 |