C + + implementation of LRU (Least recently used least recently used) (sequential table)

Source: Internet
Author: User

Recently rebuilt the process design ability, there is a sentence for my practice plan has a good guide, ability (knowledge + thinking + practice), three of the lack of the most is the practice, but also the most time-consuming. Spent the morning writing a simple LRU program

The abstract of the problem is: The default tail is just enter the cache, the head is the most recent unused

1 //the default cache for the code is 3 blocks in size2 3#include <iostream>4 5 using namespacestd;6 7 //defines the cache class, implements the order table cache, and the LRU uses the Find, delete, and insert operations8 classCache9 {Ten  Public: One     intarr[3]; A     intArrlen; -     intCurrent ; -  the cache () -     { -arr[0] =0; -arr[1] =0; +arr[2] =0; -  +Arrlen =0; ACurrent =0; at     } -  -     intFindNode (intValue//Find Nodes -     { -         if(Arrlen = =0) -         { incout <<"Cache is empty"<<Endl; -             return-1; to         } +          for(inti =0; I <3; ++i) -         { the             if(Arr[i] = =value) *             { $Current =i;Panax Notoginseng                 return 1; -             } the         } +         return 0; A  the     } +     voidDeletenode (intPosition//Delete node (current) -     { $         if(Arrlen = =0) $         { -cout <<"array empty, delete invalid"<<Endl; -             return ; the         } -         if(Position >2)Wuyi         { thecout <<"Delete location is illegal"<<Endl; -             return ; Wu         } -          for(inti = position; i < position; ++i) About         { $Arr[i] = arr[i+1]; -         } -arrlen--; -  A     } +     voidInsertnode (intVALUECC)//Insert Node (tail) the     { -         if(Arrlen! =0) $         { the              for(inti =2; i >0; --i) the             { theArr[i] = arr[i-1]; the             } -         } in         //Bank *node= New Bank; thearr[0] =VALUECC; theArrlen = Arrlen +1; About     } the }; the intMain () the { + Cache cc; -     intvalue; the      while(Cin >>value)Bayi     { the         if(Cc.arrlen <3) the         { -             if(Cc.findnode (value) = =1) -             { the Cc.deletenode (cc.current); the Cc.insertnode (value); the             } the             Else -             { the Cc.insertnode (value); the             } the 94         } the         Else the         { the             if(Cc.findnode (value) = =1)98             { About Cc.deletenode (cc.current); - Cc.insertnode (value);101             }102             Else103             {104Cc.current =2; the Cc.deletenode (cc.current);106 Cc.insertnode (value);107             }108         }109     } the      for(inti =0; I <=2; i++)111     { thecout << Cc.arr[i] <<Endl;113     } the     return 0; the}

C + + implementation of LRU (Least recently used least recently used) (sequential table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.