SSTF disk scheduling 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 |
#include <bits/stdc++.h> using namespace std; const int N=100005; int n; int head; int done[N]; int positions[N]; void sstf(void) { int movement,shortest,index; movement = 0; for(int i=0; i<n; i++) { index = 0; shortest = INT_MAX; for(int k=0; k<n; k++) { if(abs(head - positions[k]) < shortest && !done[k]) { index = k; shortest = abs(head - positions[k]); } } done[index] = true; movement += shortest; head = positions[index]; } printf("Total Head Movement %d Cylinders\n",movement); return; } int main() { cout<<"Initial Head Position: "; cin>>head; cout<<"Queue Size: "; cin>>n; cout<<"Queue:\n"; for(int i=0; i<n; i++) cin>>positions[i]; sstf(); return 0; } /** 53 8 98 183 37 122 14 124 65 67 */ |