#include<iostream>
using namespace std;
struct node
{
int data;
node *rlink,*llink;
}*t,*h,*n,*s;
class lru
{
int a,fsize,i,x;
public:
lru()
{
cout<<"\nEnter total number of caches";
cin>>a;
cout<<"\nEnter the frame size";
cin>>fsize;
h = new node;
h->rlink=NULL;
h->llink=NULL;
h->data=0;
for(i=1;i<=a;i++)
{
cout<<"\nEnter the number";
cin>>x;
if(i>=fsize)
{
del_beg ();
ins_end(x);
}
else
{
ins_end(x);
}
cout<<"\n"<<x<<"is allocated";
}
}
void ins_end(int x)
{
s=h;
while(s->rlink != NULL)
s=s->rlink;
n=new node;
n->data=x;
n->rlink=NULL;
s->llink=n;
}
void del_beg()
{
s=h->rlink;
h->rlink=s->rlink;
delete s;
}
void print()
{
s=h->rlink;
while(s != NULL)
{
cout<<s->data;
s=s->rlink;
}
}
};
int main()
{
lru r;
cout<<"\nAfter Inserting all caches";
r.print();
return 0;
}
Name | Views | Likes |
---|---|---|
Implementing LRU cache | 111 | 1 |
Comments
Ram
24-Oct-2019 08:37:42 PM