Skip to content

Commit f96f09a

Browse files
Adding C program to simulate LRU, LFU and MRU caches'
1 parent f3da595 commit f96f09a

1 file changed

Lines changed: 282 additions & 0 deletions

File tree

CacheSimulation.c

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef struct node {
5+
int data;
6+
int freq;
7+
struct node* next;
8+
9+
}Nodetype;
10+
11+
Nodetype* makenode(int data){
12+
13+
Nodetype *nptr;
14+
nptr = (Nodetype*)malloc(sizeof(Nodetype));
15+
nptr->data = data;
16+
nptr->freq = 1;
17+
nptr->next = NULL;
18+
19+
return nptr;
20+
21+
}
22+
23+
void traverse(Nodetype *lptr){
24+
25+
/*Prints node data*/
26+
27+
Nodetype *nptr;
28+
nptr = lptr;
29+
while(nptr != NULL){
30+
printf("Data: ");
31+
printf("%d ",nptr->data);
32+
printf("Frequency: ");
33+
printf("%d\t",nptr->freq);
34+
nptr = nptr->next;
35+
}
36+
printf("\n");
37+
}
38+
39+
40+
int getlength(Nodetype *lptr){
41+
42+
/*Return Length of the Cache*/
43+
44+
int length = 0;
45+
Nodetype *nptr;
46+
nptr = lptr;
47+
48+
while(nptr != NULL){
49+
50+
length++;
51+
nptr = nptr->next;
52+
}
53+
54+
return length;
55+
}
56+
57+
58+
Nodetype* exists(Nodetype *lptr,int data){
59+
60+
61+
/*Checks if a node with given data exists in cache or not */
62+
63+
Nodetype *nptr;
64+
nptr = lptr;
65+
int found = 0;
66+
67+
while(nptr != NULL && found == 0){
68+
69+
70+
if(nptr->data == data){
71+
72+
found = 1;
73+
}
74+
else{
75+
nptr = nptr->next;
76+
}
77+
78+
}
79+
80+
return nptr;
81+
82+
}
83+
84+
85+
Nodetype* InsertAtEnd(Nodetype *lptr, int data){
86+
87+
/*Creates a node and inserts it at the end*/
88+
89+
Nodetype *nptr, *n;
90+
nptr = lptr;
91+
n = makenode(data);
92+
93+
if(nptr != NULL){
94+
95+
while(nptr->next != NULL){
96+
97+
nptr = nptr->next;
98+
99+
}
100+
nptr->next = n;
101+
102+
}
103+
else{
104+
/*This case arrives only at the start where you start creating the cache*/
105+
lptr = n;
106+
107+
}
108+
109+
return lptr;
110+
111+
}
112+
113+
Nodetype* AppendAtEnd(Nodetype *lptr, Nodetype *nptr){
114+
115+
/*Appends a node which exists in cache at the end */
116+
117+
Nodetype *prev, *ptr, *last;
118+
prev = NULL;
119+
ptr = lptr;
120+
last = lptr;
121+
122+
while(last->next != NULL){
123+
last = last->next;
124+
}
125+
126+
if(nptr != last){
127+
128+
while(ptr != nptr){
129+
prev = ptr;
130+
ptr = ptr->next;
131+
}
132+
133+
if(prev != NULL){
134+
prev->next = nptr->next; //If any node other than the first node matches ,only then prev has to be taken into account
135+
}
136+
last->next = nptr;
137+
if(nptr == lptr){
138+
lptr = lptr->next; //Only if the first node matches lptr will change
139+
}
140+
nptr->next = NULL;
141+
142+
}
143+
return lptr;
144+
145+
}
146+
147+
int main() {
148+
149+
int n;
150+
int choice = 0;
151+
int data = 0;
152+
int cache_size = 0;
153+
int min;
154+
int rchoice;
155+
156+
Nodetype *lptr = NULL,*nptr = NULL,*lfu = NULL;
157+
158+
printf("Enter Cache size\n");
159+
scanf("%d",&n);
160+
if(n == 0){
161+
printf("ENTER A POSITIVE CACHE SIZE\n");
162+
}
163+
else{
164+
printf("You have the following choices :\n");
165+
printf("1.Access a Data element\n");
166+
printf("2.Exit\n");
167+
168+
while(choice != 2){
169+
170+
printf("Enter choice\n");
171+
scanf("%d",&choice);
172+
173+
switch(choice){
174+
175+
case 1:
176+
printf("Enter data element that is to be accessed\n");
177+
scanf("%d",&data);
178+
179+
180+
181+
if(cache_size < n){
182+
183+
184+
nptr = exists(lptr,data);
185+
186+
187+
if(nptr == NULL){
188+
lptr = InsertAtEnd(lptr,data);
189+
}
190+
else{
191+
//printf("\n,,%d\n",nptr->data);
192+
nptr->freq += 1;
193+
lptr = AppendAtEnd(lptr,nptr);
194+
}
195+
196+
}
197+
else{
198+
nptr = exists(lptr,data);
199+
200+
if(nptr != NULL){
201+
nptr->freq = nptr->freq + 1;
202+
lptr = AppendAtEnd(lptr,nptr);
203+
}
204+
else{
205+
printf("Replacement choices:\n");
206+
printf("1.LRU:\n");
207+
printf("2.MRU:\n");
208+
printf("3.LFU:\n");
209+
scanf("%d",&rchoice);
210+
211+
switch(rchoice){
212+
213+
case 1:/*LRU*/
214+
215+
printf("ELement removed: %d\n",lptr->data);
216+
lptr->data = data;
217+
lptr->freq = 1;
218+
lptr = AppendAtEnd(lptr,lptr);
219+
220+
break;
221+
222+
case 2:/*MRU*/
223+
224+
nptr = lptr;
225+
while(nptr->next != NULL){
226+
nptr = nptr->next;
227+
}
228+
printf("Element removed: %d\n",nptr->data);
229+
nptr->data = data;
230+
nptr->freq = 1;
231+
232+
break;
233+
234+
case 3:/*LFU*/
235+
236+
min = INT_MAX;
237+
nptr = lptr;
238+
lfu = lptr;
239+
while(nptr != NULL){
240+
if(nptr->freq < min){
241+
min = nptr->freq;
242+
lfu = nptr;
243+
}
244+
nptr = nptr->next;
245+
}
246+
printf("Element removed: %d\n",lfu->data);
247+
lfu->data = data;
248+
lfu->freq = 1;
249+
lptr = AppendAtEnd(lptr,lfu);
250+
251+
break;
252+
253+
default:
254+
255+
printf("Invalid choice\n");
256+
257+
break;
258+
259+
}
260+
}
261+
}
262+
cache_size = getlength(lptr);
263+
264+
265+
break;
266+
case 2:
267+
printf("*******************EXIT*******************");
268+
break;
269+
default:
270+
printf("Enter a valid choice\n");
271+
}
272+
273+
274+
275+
}
276+
277+
278+
}
279+
280+
getch();
281+
return 0;
282+
}

0 commit comments

Comments
 (0)