21.C#写算法之LRU算法的实现

LRU 全称 Least Recently Used,最近最少使用。

using System;
using System.Collections.Generic;

//LRU-Least recently used
namespace Test01
{
    class Node
    {
        public string key;
        public string value;
        public Node pre;
        public Node next;
        public Node(string key, string value)
        {
            this.key = key;
            this.value = value;
        }
    }

    class LRUCache
    {
        private Node head;
        private Node end;
        private int limit;
        private Dictionary<string, Node> dict;
        
        public LRUCache(int limit)
        {
            this.limit = limit;
            dict = new Dictionary<string, Node>();
        }

        public string Get(string key)
        {
            if(!dict.TryGetValue(key, out Node node))
            {
                return null;
            }
            RefreshNode(node);
            return node.value;
        }

        public void Put(string key, string value)
        {
            if(!dict.TryGetValue(key, out Node node))
            {
                if(dict.Count >= limit)
                {
                    string oldKey = RemoveNode(head);
                    dict.Remove(oldKey);
                }
                node = new Node(key, value);
                AddNode(node);
                dict.Add(key, node);
            }
            else
            {
                node.value = value;
                RefreshNode(node);
            }            
        }

        public void Remove(string key)
        {
            if(dict.TryGetValue(key, out Node node))
            {
                RemoveNode(node);
                dict.Remove(key);
            }
        }

        private void RefreshNode(Node node)
        {
            if (node == end)
                return;
            RemoveNode(node);
            AddNode(node);
        }

        private void AddNode(Node node)
        {
            if (end != null)
            {
                end.next = node;
                node.pre = end;
                node.next = null;
            }
            end = node;
            if (head == null)
                head = node;
        }

        private string RemoveNode(Node node)
        {
            if (node == head && node == end)
            {
                head = null;
                end = null;
            }else if (node == end)
            {
                end = end.pre;
                end.next = null;
            }else if (node == head)
            {
                head = head.next;
                head.pre = null;
            }
            else
            {
                node.pre.next = node.next;
                node.next.pre = node.pre;
            }
            return node.key;
        }

        static void Main(string[] args)
        {
            LRUCache lRUCache = new LRUCache(5);
            lRUCache.Put("001", "User1 - Message");
            lRUCache.Put("002", "User2 - Message");
            lRUCache.Put("003", "User3 - Message");
            lRUCache.Put("004", "User4 - Message");
            lRUCache.Put("005", "User5 - Message");
            lRUCache.Get("002");
            lRUCache.Put("004", "User4 - NewMessage");
            lRUCache.Put("006", "User6 - UpdateMessage");
            Console.WriteLine(lRUCache.Get("001"));
            Console.WriteLine(lRUCache.Get("006"));
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值