-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU gfg.txt
89 lines (40 loc) · 1.32 KB
/
LRU gfg.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
public class LRU {
// Driver method
public static void main(String[] args) {
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
// To represent set of current pages.We use
// an Arraylist
ArrayList<Integer> s=new ArrayList<>(capacity);
int count=0;
int page_faults=0;
for(int i:arr)
{
// Insert it into set if not present
// already which represents page fault
if(!s.contains(i))
{
// Check if the set can hold equal pages
if(s.size()==capacity)
{
s.remove(0);
s.add(capacity-1,i);
}
else
s.add(count,i);
// Increment page faults
page_faults++;
++count;
}
else
{
// Remove the indexes page
s.remove((Object)i);
// insert the current page
s.add(s.size(),i);
}
}
System.out.println(page_faults);
}
}