SCAN Disk Scheduling algorithm Program in C++ language .
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 |
#include <bits/stdc++.h> using namespace std; const int N=100005; int n; int head; int direction; int done[N]; int positions[N]; void scan(void) { int movement,best,index,complete; movement = 0; complete = 0; while(complete < n) { index = -1; if(direction == 0) best = INT_MIN; if(direction == 1) best = INT_MAX; for(int i=0; i<n; i++) { if(!done[i]) { if(direction == 0 && positions[i] > best && positions[i] < head) { index = i; best = positions[i]; } if(direction == 1 && positions[i] < best && positions[i] > head) { index = i; best = positions[i]; } } } if(index >= 0) { complete++; done[index] = true; movement += abs(head - best); head = positions[index]; } else { direction = 1 - direction; } } cout<<"Total Head Movement "<<movement<<" Cylinders\n"; return; } int main() { cout<<"Initial Head Position: "; cin>>head; cout<<"Initial Direction [0/1]: "; cin>>direction; cout<<"Queue Size: "; cin>>n; cout<<"Queue:\n"; for(int i=0; i<n; i++) cin>>positions[i]; scan(); return 0; } /** 53 0 8 98 183 37 122 14 124 65 67 */ |