container

heap

  1. import (
  2. "container/heap"
  3. "fmt"
  4. )
  5. // An IntHeap is a min-heap of ints.
  6. type IntHeap []int
  7. func (h IntHeap) Len() int { return len(h) }
  8. func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
  9. func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  10. // 末尾添加元素
  11. func (h *IntHeap) Push(x interface{}) {
  12. *h = append(*h, x.(int))
  13. }
  14. // 末尾删除元素
  15. func (h *IntHeap) Pop() interface{} {
  16. old := *h
  17. n := len(old)
  18. x := old[n-1]
  19. *h = old[0 : n-1]
  20. return x
  21. }
  22. func main() {
  23. h := &IntHeap{2, 1, 5}
  24. heap.Init(h)
  25. heap.Push(h, 3)
  26. fmt.Printf("minimum: %d\n", (*h)[0]) // 小根堆
  27. for h.Len() > 0 {
  28. fmt.Printf("%d ", heap.Pop(h))
  29. }
  30. }

list

  1. import (
  2. "container/list"
  3. "fmt"
  4. )
  5. func main() {
  6. // 创建一个新的链表
  7. l := list.New()
  8. e2 := l.PushBack(2)
  9. e5 := l.PushBack(5)
  10. l.InsertBefore(3, e2)
  11. l.InsertAfter(19, e5)
  12. for e := l.Front(); e != nil; e = e.Next() {
  13. fmt.Println(e.Value)
  14. }
  15. }

list 构建 queue/stack

  1. // queue
  2. queue := list.New()
  3. // queue push
  4. queue.pushBack()
  5. // queue pop
  6. front := queue.Front()
  7. queue.Remove(front)
  8. // stack
  9. stack := list.New()
  10. // stack push
  11. stack.pushBack()
  12. // stack pop
  13. back := stack.Back()
  14. stack.Remove(back)

由于 stack 是在一端进行增加删除,所以使用切片来模拟 stack 会更加方便。

  1. // stack
  2. stack := []int{}
  3. // stack push
  4. stack = append(stack, elm)
  5. // stack pop
  6. stack = stack[:len(stack)-1]

LRU

  1. package lru
  2. type LRU struct {
  3. cap int
  4. pos map[int]*Element
  5. root Element // root.next = first, root.pre = last
  6. }
  7. // Element a list element
  8. type Element struct {
  9. Value int
  10. next, pre *Element
  11. }
  12. // New returns an initialized list
  13. func New(cap int) *LRU {
  14. return new(LRU).Init(cap)
  15. }
  16. // Init initializes the lru list
  17. func (lru *LRU) Init(cap int) *LRU {
  18. lru.root.next = &lru.root
  19. lru.root.pre = &lru.root
  20. lru.cap = cap
  21. lru.pos = make(map[int]*Element)
  22. return lru
  23. }
  24. // Query visit a value
  25. func (lru *LRU) Query(v int) {
  26. if lru.pos[v] != nil {
  27. lru.toLast(lru.pos[v])
  28. } else {
  29. if len(lru.pos) == lru.cap {
  30. lru.pop()
  31. }
  32. lru.push(v)
  33. }
  34. }
  35. func (lru *LRU) toLast(elm *Element) {
  36. elmPre, elmNext := elm.pre, elm.next
  37. elmPre.next = elmNext
  38. elmNext.pre = elmPre
  39. curLast := lru.root.pre
  40. curLast.next = elm
  41. elm.pre = curLast
  42. elm.next = &lru.root
  43. lru.root.pre = elm
  44. }
  45. // remove least visit element, which is the head of the list
  46. func (lru *LRU) pop() int {
  47. head := lru.root.next
  48. lru.root.next = head.next
  49. head.next.pre = &lru.root
  50. delete(lru.pos, head.Value)
  51. return head.Value
  52. }
  53. func (lru *LRU) push(v int) {
  54. elm := &Element{
  55. Value: v,
  56. }
  57. curLast := lru.root.pre
  58. curLast.next = elm
  59. lru.root.pre = elm
  60. elm.next = &lru.root
  61. elm.pre = curLast
  62. lru.pos[v] = elm
  63. }