问题描述
- 用单链表实现磁盘调度算法的时候,最短寻道时间算法不知道哪错了
-
#include
#include
#include
#define Max 5typedef struct DiskLine
{//磁盘请求链表
int Location;//磁盘请求位置
DiskLine *next;//指向下一节点的指针
}DiskLine;//初始化磁盘链表
void InitList(DiskLine *&L)
{
L=(DiskLine *)malloc(sizeof(DiskLine));
L->next=NULL;
}void CreateLine(DiskLine *&L,int a[],int n)
{
DiskLine *s,*r;
int i;
//L=(DiskLine *)malloc(sizeof(DiskLine));
r=L;
for(i=0;i<=n;i++)
{
s=(DiskLine *)malloc(sizeof(DiskLine));s->Location=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}void Delet(DiskLine *L)
{//销毁磁盘请求链表
DiskLine *p,*q;
for(p = L; p != NULL; )
{
q = p;
p = p->next;
free(q);
}
p = q =NULL;
}//Deletvoid FCFS(DiskLine *L)
{
//先进先出算法
DiskLine *temp1,*temp2;
temp2 = L;
printf("%dn",temp2->Location);
int i=0,Sum = 0;
printf("先进先出算法调度的顺序:n");
for( temp1 = L->next; temp1->next!= NULL; temp1 = temp1->next, temp2 = temp2->next)
{
//顺序输出
printf("%d ",temp1->Location);
Sum += abs(temp1->Location - temp2->Location);
i++;
}
printf("n");
printf("磁盘的寻道长度为%dn",Sum);
printf("磁盘的平均寻道长度为%dn",Sum/i);
}//FCFSint SSTF(DiskLine *L)
{//最短寻道时间优先算法
DiskLine *L2;
DiskLine *p,*q1,*q2;
DiskLine *temp1,*temp,*temp2;
int min,Sum = 0;
//按照距离上一次磁头位置最近的顺序重新排列调度请求链表
L2 = (DiskLine *)malloc(sizeof(DiskLine));//重排列后的链表头节点
L2->Location = L->Location;//初始位置
L2->next = NULL;
q2 = L2;
temp1 = L;
for(temp2= L; temp2->next != NULL; temp2=temp2->next)
{
min =abs(temp2->next->Location -temp1->Location);
for(p = L; p->next != NULL; p = p->next)
{//从原链表中选取距离磁头最近的请求位置,将其插入L2的链尾
if(abs(p->next->Location - temp1->Location) < min)
{
min = abs(p->next->Location - temp1->Location);
q1 = p->next;
}
}//将节点从原链表删除 q2->next = q1->next; q1->next = q1->next->next; q2->next->next = NULL; Sum += min; /*if(L->next == NULL) break;//退出循环条件*/ }// for(temp = L2->next;temp->next!= NULL; temp = temp->next) {//输出重排列的链表 printf(" %d ",temp->Location); } L = L2;//便于销毁操作 return Sum;
}
int SCAN(DiskLine *L)
{//扫描算法
DiskLine *L2,*L3;
DiskLine *p2,*p3;
int Sum = 0,Sum1,Sum2;
DiskLine *temp = L->next;
L2 = (DiskLine *)malloc(sizeof(DiskLine));
L2->Location = 53;
L2->next = NULL;
p2 = (DiskLine *)malloc(sizeof(DiskLine));
//保证磁头可到达0位置
p2->Location = 0;
p2->next = L2->next;
L2->next = p2;
L3 = (DiskLine *)malloc(sizeof(DiskLine));
L3->Location = 0;
L3->next = NULL;
p2 = L2->next;
p3 = L3;
while(temp != NULL)
{
if((temp->Location - L->Location) <= 0)
{//选出原链表中请求位置小于等于磁头初始位置的节点插入L2链尾
p2->next = temp;
temp = temp->next;
p2 = p2->next;
p2->next = NULL;}
else
{//选出原链表中请求位置大于磁头初始位置的节点插入L3链尾
p3->next = temp;
temp = temp->next;
p3 = p3->next;
p3->next = NULL;
}
}
Sum1=SSTF(L2);//对链表L2调用SSTF算法
Sum2=SSTF(L3);//对链表L3调用SSTF算法
Sum=Sum1 + Sum2;
return Sum;
}//SCANint C_SCAN(DiskLine *L)
{//循环扫描算法
DiskLine *L2,*L3;
DiskLine *p2,*p3;
int Sum = 0,Sum1,Sum2;
DiskLine *temp = L->next;
L2 = (DiskLine *)malloc(sizeof(DiskLine));
L2->Location = 0;
L2->next = NULL;
//保证磁头能到达0位置
p2 = (DiskLine *)malloc(sizeof(DiskLine));
p2->Location = 0;
p2->next = L2->next;
L2->next = p2;
L3 = (DiskLine *)malloc(sizeof(DiskLine));
L3->Location = 53;
L3->next = NULL;
//保证磁头能到达200位置
p3 = (DiskLine *)malloc(sizeof(DiskLine));
p3->Location = 200;
p3->next = L3->next;
L3->next = p3;
p2 = L2->next;
p3 = L3->next;
while(temp != NULL)
{//同SCAN
if((temp->Location - L->Location) <= 0)
{
p2->next = temp;
temp = temp->next;
p2 = p2->next;
p2->next = NULL;}
else
{
p3->next = temp;
temp = temp->next;
p3 = p3->next;
p3->next = NULL;}
}
Sum1 = SSTF(L3);
Sum2 = SSTF(L2);
Sum = Sum1 + Sum2;
return Sum;
}//C_SCANvoid main()
{
DiskLine *L,*r;
InitList(L);
int choice,Queue[Max];
printf("----------------------磁盘调度模拟----------------n");
printf("请输入磁盘请求的个数:");
printf("%dn",Max);
printf("-----------------------初始状态-------------------n");
printf("nn");
printf("磁头初始位置为:");
scanf("%d",&L->Location);
printf("磁盘调度请求队列:n");for(int i=0;i<Max;i++) scanf("%d",&Queue[i]); CreateLine(L,Queue,Max); printf("请选择调度算法:n"); printf(" 1 为FCFS<先来先服务调度法>;n"); printf(" 2 为SSTF<最短寻道时间优先扫描算法>;n"); printf(" 3 为SCAN<扫描算法>;n"); printf(" 4 为C_SCAN<循环扫描算法>;n"); printf(" 5 为退出;n"); printf("请输入你的选择:"); scanf("%d",&choice); switch(choice) { case 1:FCFS(L);break; case 2:SSTF(L);break; case 3:SCAN(L);break; case 4:C_SCAN(L);break; case 5:break; default:printf("ERROR!"); }
}
注:这是老师给的算法,但是基本上都有问题,目前SSTF被我改成这样,我真的不知道哪儿错了,求指点