LRU page replacement algorithm JAVA Implementation

Source: Internet
Author: User

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<Br/> Import Java. util. using list; <br/> Import Java. util. list; <br/> Import Java. util. volume; </P> <p> public class LRU {<br/> static int volum; // stack capacity <br/> static list <integer> List = new consumer list <integer> (); <br/> // The linked list is used to simulate the stack storage page <br/> static int [] visit; // The page array to be accessed <br/> static int COUNT = 0; // record page missing Times <br/> Public static void main (string [] ARGs) <br/>{< br/> Using SC = new using (system. in); <br/> system. O Ut. print ("Enter the stack capacity:"); <br/> volum = SC. nextint (); </P> <p> system. out. print ("Enter the total number of pages:"); <br/> int n = SC. nextint (); </P> <p> visit = new int [N]; <br/> system. out. println ("Enter the page number of each page:"); <br/> for (INT I = 0; I <n; I ++) <br/> visit [I] = SC. nextint (); <br/> slru (); // call the algorithm that has not been used for the last time </P> <p> system. out. println ("the number of replacement pages is:" + count); </P> <p >}< br/> Public static void slru () <br/>{< br/> int Index = 0; <br/> while (index <visit. length) <br/> {<Br/> Boolean flag = false; <br/> If (list. size () <= volum) <br/> {<br/> for (INT I = 0; I <list. size (); I ++) <br/>{< br/> If (INT) (List. get (I) = visit [Index]) <br/>{< br/> list. remove (I); // Delete <br/> list. add (visit [Index]); // Add it to the end <br/> flag = true; <br/> break; <br/>}< br/> If (! Flag) <br/>{< br/> If (list. size () <volum) <br/> {// If the stack is not full and the page is not in the stack, add it to the stack <br/> list. add (visit [Index]); </P> <p >}< br/> else <br/> {// If the stack is full, if the page number is not in the stack, delete the bottom element of the stack and insert a new page. <br/> int temp = List. get (0); <br/> list. remove (0); // Replace the first one <br/> list. add (visit [Index]); // Add it to the end <br/> count ++; <br/> system. out. println ("start to switch pages, and" + temp + "change out" at the bottom of the stack); <br/> system. out. println ("this is also impossible, after all, stack is limited"); <br/>}</P> <p >}< br/> system. out. print ("after the" + (index + 1) + "Page stack content is"); <br/> for (int K = 0; k <list. size (); k ++) <br/> system. out. print (list. get (k) + ""); <br/> system. out. println (); <br/> index ++; <br/>}< br/> 

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.