Linked file allocation program in C.
Program :
#include<stdio.h>
int main(){
int f[50],i,st,len,op,ind,n;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks are already allocated:\n");
scanf("%d",&n);
printf("Enter blocks already allocated:\n");
for(i=0;i<n;i++){
scanf("%d",&ind);
f[ind]=1;
}
x:
printf("Enter the index of starting block and length:\n");
scanf("%d%d",&st,&len);
if(f[st] == 0){
for(i=st;i<(st+len);i++){
if(f[i] == 0){
f[i] = 1;
printf("%d --> %d \n",i,f[i]);
}else{
printf("Block %d already allocated.\n",i);
}
}
}else
printf("%d starting block is already allocated.\n",st);
printf("Press 1 to continue.....\n");
scanf("%d",&op);
if(op == 1)
goto x;
return 0;
}
Output:
Enter how many blocks are already allocated:
3
Enter blocks already allocated:
1
3
5
Enter the index of starting block and length:
4
2
4 --> 1
Block 5 already allocated.
Press 1 to continue.....
1
Enter the index of starting block and length:
6
1
6 --> 1
Press 1 to continue.....
0
#include<stdio.h>
int main(){
int f[50],i,st,len,op,ind,n;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks are already allocated:\n");
scanf("%d",&n);
printf("Enter blocks already allocated:\n");
for(i=0;i<n;i++){
scanf("%d",&ind);
f[ind]=1;
}
x:
printf("Enter the index of starting block and length:\n");
scanf("%d%d",&st,&len);
if(f[st] == 0){
for(i=st;i<(st+len);i++){
if(f[i] == 0){
f[i] = 1;
printf("%d --> %d \n",i,f[i]);
}else{
printf("Block %d already allocated.\n",i);
}
}
}else
printf("%d starting block is already allocated.\n",st);
printf("Press 1 to continue.....\n");
scanf("%d",&op);
if(op == 1)
goto x;
return 0;
}
Output:
Enter how many blocks are already allocated:
3
Enter blocks already allocated:
1
3
5
Enter the index of starting block and length:
4
2
4 --> 1
Block 5 already allocated.
Press 1 to continue.....
1
Enter the index of starting block and length:
6
1
6 --> 1
Press 1 to continue.....
0
Comments
Post a Comment