第十天:golang学习笔记之container

Macmillan_ · · 552 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

★container | heap堆操作, list双向链表,ring环形链表

现在网上的算法题,很少有可以用golang作答的,不过看看别人造的轮子还是挺有意思的
container本身并不是包,但目录下包括三个子包:heap,list,ring

heap

  1. 包含一个Interface接口,接口定义了Push(x interface{}),Pop() interface{} ,并包含了sort.Interface接口。
  2. 这里的堆是小顶堆
  3. heap并没有可用的实现,但在test中找到一个:
// 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
}
  1. 在进行heap.Pop,heap.Remove,heap.Push时,会自动fix为小顶堆

PS:所以堆拍啥的只要pop() pop() pop()就可以了

list ring

这俩一个尿性放一块看了
list是有头的所以有type List structtype Element struct,Element是双向的并且有*List指针
ring是一个固定大小的环,仅由type Ring struct组成


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:Macmillan_

查看原文:第十天:golang学习笔记之container

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

552 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传