/*
TO IMPLEMENT LRU IN JAVA
*/
import java.util.*;
class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
void insert(int d)
{
if(f!=3)
{
if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}
else
{
f=0;
if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}
System.out.print("\nTop:"+f);
}
boolean find(int d)
{
if(count==3) count=2;
for(int i=0;i<=count;i++)
{
if(d==q[i] && count>=2)
return true;
}
return false;
}
void traverse()
{
if(count==3) count=2;
for(int i=0;i<=count;i++)
{
System.out.print("\n"+q[i]);
}
if(count<3) count++;
System.out.println();
}
}
class LRU
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}
No comments:
Post a Comment