Skip to main content

Simple LRU Cache Implementation using C#

        


        public class Node

    {

        public int Key, Value;

        public Node Prev, Next;

        public Node(int key, int value)

        {

            this.Key = key;

            this.Value = value;

        }

    }

 

    public class CustomDoubleLinkedList

    {

        Node Head;

        Node Tail;

        public CustomDoubleLinkedList()

        {

            this.Head = new Node(0, 0);

            this.Tail = new Node(0, 0);

            this.Head.Next = this.Tail;

            this.Tail.Prev = this.Head;

        }

        public void AddToHead(Node currNode)

        {

            currNode.Prev = this.Head;

            currNode.Next = this.Head.Next;

            this.Head.Next.Prev = currNode;

            this.Head.Next = currNode;

        }

 

        public void PromoteToHead(Node currNode)

        {

            RemoveNode(currNode);

            AddToHead(currNode);

        }

 

        public void RemoveNode(Node currNode)

        {

            currNode.Prev.Next = currNode.Next;

            currNode.Next.Prev = currNode.Prev;

        }

 

        public void RemoveTail()

        {

            Node newTail = GetTailNode().Prev;

            newTail.Next = this.Tail;

            this.Tail.Prev = newTail;

        }

 

        public Node GetTailNode()

        {

            return this.Tail.Prev;

        }

 

    }

 

    public class LRUCache

    {

        CustomDoubleLinkedList DoubleLinkList;

        Dictionary<int, Node> cacheMap;

        int Capacity;

 

        public LRUCache(int capacity)

        {

            DoubleLinkList = new CustomDoubleLinkedList();

            this.Capacity = capacity;

            this.cacheMap = new Dictionary<int, Node>();

        }

 

        public int Get(int key)

        {

            if (cacheMap.ContainsKey(key))

            {

                Node CurrNode = cacheMap[key];

                int currValue = CurrNode.Value;

                DoubleLinkList.PromoteToHead(CurrNode);

                return currValue;

            }

            return -1;

        }

 

        public void Set(int key, int value)

        {

            if (cacheMap.ContainsKey(key))

            {

                Node hitNode = cacheMap[key];

                hitNode.Value = value;

                DoubleLinkList.PromoteToHead(hitNode);

            }

            else

            {

                Node newNode = new Node(key, value);

                if (cacheMap.Count == this.Capacity)

                {

                    Node tailNode = this.DoubleLinkList.GetTailNode();

                    this.cacheMap.Remove(tailNode.Key);

                    this.DoubleLinkList.RemoveTail();

                }

                cacheMap.Add(key, newNode);

                this.DoubleLinkList.AddToHead(newNode);

            }

        }

    }

 

    public class LRUCacheDemo

    {

        public static void LRUCacheDemoStart()

        {

            LRUCache cache = new LRUCache(5);

            cache.Set(1, 100);

            cache.Set(2, 200);

            cache.Set(3, 300);

            cache.Set(4, 400);

            cache.Set(5, 500);

            Console.WriteLine(cache.Get(4));

        }

    }

Comments

Popular posts from this blog

Git fundamental and useful command to use - version-2

      Install Git your local machine Link: Git - Downloads (git-scm.com) Version : git version 2.31.1.windows.1   Know the version $ git --version   Git help for command $ git help config $ git config --help Prompt the git config help file in browser Make a directory $ mkdir shubhapp$ mkdir shubhapp $ cd shubhapp/ - go to directory   initialize the git $ git init Initialized empty Git repository in C:/Users/shubh/shubhapp/.git/   Create a file Hello.txt   Add this file to git $ git Add hello.txt   Commit the changes $ git commit -a -m "commit comment"   Connect to remote github $ git config --global user.username shubh-techie  

Min Cost to Connect Ropes problem solution using PriorityQueue using C#

Given n ropes of different lengths, we need to connect these ropes into one rope. We can connect only 2 ropes at a time. The cost required to connect 2 ropes is equal to sum of their lengths. The length of this connected rope is also equal to the sum of their lengths. This process is repeated until n ropes are connected into a single rope. Find the min possible cost required to connect all ropes. Input: ropes = [ 8 , 4 , 6 , 12 ] Output: 58 Explanation: The optimal way to connect ropes is as follows 1. Connect the ropes of length 4 and 6 (cost is 10 ). Ropes after connecting: [ 8 , 10 , 12 ] 2. Connect the ropes of length 8 and 10 (cost is 18 ). Ropes after connecting: [ 18 , 12 ] 3. Connect the ropes of length 18 and 12 (cost is 30 ). Total cost to connect the ropes is 10 + 18 + 30 = 58 public static void Demo() { //int[] Ropes = { 4, 3, 2, 6 }; //int[] Ropes = { 8, 4, 6, 12 }; int[] Ropes = { 1, 2, 5, 10, 35, 89

Given an array of integers, and a target value determine if there are two integers that add to the sum using C#

Given an array of integers, and a target value determine if there are two integers that add to the sum.