-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFIFO.cpp
100 lines (96 loc) · 1.63 KB
/
FIFO.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
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
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<limits.h>
using namespace std;
struct queue
{
int front,rear,size,capacity;
int* array;
};
struct queue* create(int cap)
{
struct queue* q=new queue;
q->capacity=cap;
q->front=q->size=0;
q->rear=-1;
q->array=new int[q->capacity];
for(int i=0;i<q->capacity;i++)
q->array[i]=-1;
return q;
}
bool isfull(struct queue* q)
{
if(q->size==q->capacity)
return true;
else
return false;
}
bool isempty(struct queue* q)
{
if(q->size==0)
return true;
else
return false;
}
void enq(struct queue* q,int item)
{
if(isfull(q))
return;
q->rear=(q->rear+1)%q->capacity;
q->array[q->rear]=item;
q->size=q->size+1;
}
int deq(struct queue* q)
{
if(isempty(q))
return INT_MIN;
int item=q->array[q->front];
q->front=(q->front+1)%q->capacity;
q->size=q->size-1;
return item;
}
bool find(int* arr,int n,int x)
{
for(int i=0;i<n;i++)
{
if(arr[i]==x)
return true;
else
continue;
}
return false;
}
int main()
{
int page_fault=0;
struct queue* q=create(3);
int str[]={7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1};
int x=sizeof(str)/sizeof(str[0]);
for(int i=0;i<x;i++)
{
bool state=find(q->array,q->capacity,str[i]);
if(state==true){
cout<<".\n\n";
continue;
}
else
{
if(isfull(q))
{
deq(q);
enq(q,str[i]);
for(int j=0;j<q->size;j++)
cout<<q->array[j]<<" ";
page_fault++;
}
else
{
enq(q,str[i]);
for(int j=0;j<q->size;j++)
cout<<q->array[j]<<" ";
page_fault++;
}
}
cout<<"\n";
}
cout<<"\nTotal no. of page fault is: "<<page_fault<<"\n";
}