Professional Documents
Culture Documents
DSC Self
DSC Self
Problem Statement:
Implementing a Data Structure for Least Recently Used (LRU) cache which supports the
following operations:
Get_from_cache (Key) : Get the value associated with the key ,if key not found report it
accordingly.
Put_into_cache (Key, Value) : Put (Key, Value) into cache. If cache is full, then use LRU
eviction policy to replace given item in place of Least Recently Used Item.
1. Fixed Size: Cache needs to have some bounds to limit memory usages.
2. Fast Access: Cache Insert and lookup operation should be fast, preferably O (1) time.
3. Replacement of Entry in case, Memory Limit is reached: A cache should have efficient
algorithm to evict the entry when memory is full.
In case of LRU cache we evict least recently used entry so we have to keep track of recently
used entries, entries which have not been used from long time and which have been used
recently. plus, lookup and insertion operation should be fast enough.
The Cache implemented here can store 32 key-value pairs.
Using both these data structures both lookup, addition and replacement of a key can be
done in constant time.
Algorithm :
For Put_into_cache (key, value):
If entry is not present in the Hash Table then add entry to Hash table and add a node
to the top of doubly linked list.
Else if entry is already present in the hash table, move the node corresponding to the
key-value pair to the top of the list.
If table is full, delete last node in the doubly linked list and add new entry to the top
of the list.
For Get_from_cache (Key):
CODE:
#include<stdio.h>
#include<stdlib.h>
#define size 32
struct dlist{
int key,val;
};
struct entry{
int k;
};
int prime=29;
return (prime-key%prime);
temp=getnode();
temp->next=temp->prev=NULL;
temp->val=value;
temp->key=ke;
if(h==NULL)
h=temp;
h->prev=h;
return h;}
temp->next=h;
nd=h->prev;
h->prev=temp;
temp->prev=nd;
h=temp;
return h;
temp=h->prev;
h->prev=temp->prev;
h->prev->next=NULL;
free(temp);
return h;
for(i=0;i<size ;i++)
index=(c + i*hash(c))%size;
if(a[index].k==c)
flag=1;
return index; }
if(flag==0)
return -1;
for(i=0;i<size; i++)
index=(ky + i*hash(ky))%size;
flag=1;
a[index].k=ky;
h=addfront(ky,value);
a[index].ne=h;
break;
flag=1;
temp=a[index].ne;
if(temp==h)
{ temp->val=value;
break;}
f=h->prev;
f=f->prev;
f->next=NULL;
temp->next=h;
h=temp;
temp->val=value;
return h;
h->next=NULL;
temp->next=h;
h->prev=temp;
temp->prev=h;
h=temp;
a[i].ne=h;
return h;
pv=temp->prev;
nt=temp->next;
pv->next=nt;
nt->prev=pv;
a[index].ne->next=h;
g=h->prev;
h->prev=a[index].ne;
a[index].ne->prev=g;
h=a[index].ne;
if(h->val!=value)
h->val=value;
return h;
index=search(a, h->prev->key);
a[index].k=ky;
a[index].ne=h;
h=deleterear ();
return h;
}
struct dlist *Get_from_cache(struct entry a[],int key)
if(index==-1)
return h; }
if(temp==h)
return h;
f=h->prev;
a[index].ne->prev->next=NULL;
a[index].ne->next=h;
h->prev=a[index].ne;
a[index].ne->prev=f;
h=a[index].ne;
return h;
h->next=NULL;
a[index].ne->next=h;
h->prev=a[index].ne;
a[index].ne->prev=h;
h=a[index].ne;
return h;
}
struct dlist *pv, *nt, *g;
pv=temp->prev;
nt=temp->next;
pv->next=nt;
nt->prev=pv;
a[index].ne->next=h;
g=h->prev;
h->prev=a[index].ne;
a[index].ne->prev=g;
h=a[index].ne;
return h;
void printcache ()
int i=1;
temp=h;
int main(){
{ a[i].k=-1;
a[i].ne=NULL; }
n2 = rand() % 50 + 1;
h=put_into_cache(a,n1,n2);
printcache();
printf("\n");
while(1)
printf("1) PUT A VALUE INTO CACHE\n2) GET A VALUE FROM CACHE\n3) PRINT THE
CURRENT STATE OF CACHE\n4) EXIT\n");
scanf("%d",&ch);
switch(ch)
scanf("%d%d",&n1,&n2);
h=put_into_cache(a,n1,n2);
break;
scanf("%d",&ke);
h=get_from_cache(a,ke);
break;
printcache();
printf("\n");
break;
case 4: exit(0);
default:exit(0); } }
return 0;
}
OUTPUT: