sstf算法c语言实现
时间: 2023-12-07 19:40:01 浏览: 108
算法:c语言实现
以下是sstf算法的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int n, head, i, j, seek_time = 0, min, pos;
printf("Enter the number of requests: ");
scanf("%d", &n);
int queue[n], visited[n];
printf("Enter the requests: ");
for(i = 0; i < n; i++)
{
scanf("%d", &queue[i]);
visited[i] = 0;
}
printf("Enter the initial head position: ");
scanf("%d", &head);
visited[head] = 1;
printf("%d -> ", head);
for(i = 0; i < n - 1; i++)
{
min = INT_MAX;
for(j = 0; j < n; j++)
{
if(!visited[j] && abs(head - queue[j]) < min)
{
min = abs(head - queue[j]);
pos = j;
}
}
visited[pos] = 1;
seek_time += min;
head = queue[pos];
printf("%d -> ", head);
}
printf("End\n");
printf("Total seek time: %d\n", seek_time);
return 0;
}
```
阅读全文