链表的应用(进阶)——LRU 缓存
type NodeList struct {
val int
key int
pre *NodeList
next *NodeList
}
func (c *NodeList) remove() {
c.next.pre = c.pre
c.pre.next = c.next
}最后更新于
type NodeList struct {
val int
key int
pre *NodeList
next *NodeList
}
func (c *NodeList) remove() {
c.next.pre = c.pre
c.pre.next = c.next
}最后更新于
type LRUCache struct {
// 链表表头
head *NodeList
// 链表表尾
tail *NodeList
// 容量
capacity int
// 缓存map
cache map[int]*NodeList
}
func(c *LRUCache) setHeader(node *NodeList) {
c.head.next.pre = node
node.next = c.head.next
node.pre = c.head
c.head.next = node
}
// LRU 创建函数
func Constructor(capacity int) LRUCache {
lru := LRUCache{head: new(NodeList),
tail: new(NodeList),
capacity: capacity,
cache: make(map[int]*NodeList, capacity)}
lru.tail.pre = lru.head
lru.head.next = lru.tail
return lru
}func (this *LRUCache) Get(key int) int {
if n, ok := this.cache[key]; !ok {
return -1
} else {
n.remove()
this.setHeader(n)
return n.val
}
}func (this *LRUCache) Put(key int, value int) {
if node, ok := this.cache[key]; ok {
node.remove()
delete(this.cache, key)
} else if len(this.cache) >= this.capacity {
toRemove := this.tail.pre
this.tail.pre = nil
toRemove.next = nil
delete(this.cache, toRemove.key)
this.tail = toRemove
}
newNode := &NodeList{val:value, key:key}
this.setHeader(newNode)
this.cache[key] = newNode
}