-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_stack.cpp
61 lines (53 loc) · 841 Bytes
/
lru_stack.cpp
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int f,n;
cout<<"Enter number of frames\n";
cin>>f;
cout<<"Enter length of string\n";
cin>>n;
int string[n];
vector<int> cache, temp;
cout<<"Enter the string\n";
for(int i=0; i<n; i++)
{
cin>>string[i];
}
int it;
for(int i=0; i<n; i++)
{
bool flag=true;
for(int j=0; j<cache.size(); j++)
{
if(cache[j] == string[i])
{
it = j;
flag = false;
break;
}
}
if(flag == true)
{
if(cache.size() < f)
cache.push_back(string[i]);
else
{
cache.erase(cache.begin());
cache.push_back(string[i]);
}
}
if(flag == false)
{
cache.erase(cache.begin() + it);
cache.push_back(string[i]);
}
temp = cache;
for(int k=0; k<temp.size(); k++)
{
cout<<temp[k]<<" ";
}
cout<<endl;
}
return 0;
}