Advertisement
4doorsmorehories

Linked File Allocation

Nov 22nd, 2022
52
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | Source Code | 0 1
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6.         int mloc;
  7.         struct node *next;
  8. };
  9. struct node *newnode = NULL, *ptr = NULL;
  10.  
  11. void main()
  12. {
  13.         int n, i, j, k, fb[50], mb[100];
  14.         struct node* ll[50];
  15.         printf("Enter the number of files: ");
  16.         scanf("%d", &n);
  17.         printf("Enter the number of blocks for each file: \n");
  18.         for(i = 0; i < n; i++)
  19.         {
  20.                 printf("File %d: ", i+1);
  21.                 scanf("%d", &fb[i]);
  22.                 ll[i] = NULL;
  23.         }
  24.         for(i = 0; i < 100; i++)
  25.                 mb[i] = rand()%2;
  26.         //printf("The memory block: \n");
  27.         printf("\nDISK:\n\n\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\n");
  28.         for (i = 0; i<100; i++){
  29.                 if (i % 10 == 0)
  30.                         printf("\n%d\t", i);
  31.                 printf("%d\t", mb[i]);
  32.  
  33.         }
  34.         printf("\n");
  35.  
  36.         for(i = 0; i < n; i++)
  37.         {
  38.                 k = 0;
  39.                 for(j = 0; j < 100; j++)
  40.                 {
  41.                         if(mb[j] == 0)
  42.                         {
  43.                                 newnode = (struct node*)malloc(sizeof(struct node));
  44.                                 newnode -> mloc = j;
  45.                                 newnode -> next = NULL;;
  46.                                 if(ll[i] == NULL)
  47.                                         ll[i] = newnode;
  48.                                 else
  49.                                 {
  50.                                         ptr = ll[i];
  51.                                         while(ptr -> next != NULL)
  52.                                                 ptr = ptr -> next;
  53.                                         ptr -> next = newnode;
  54.                                 }
  55.                                 mb[j] = 1;
  56.                                 k++;
  57.                                 if(k == fb[i])
  58.                                         break;
  59.                         }
  60.                 }
  61.         }
  62.         printf("\n\nLinked lists are: ");
  63.         for(i = 0; i < n; i++)
  64.         {
  65.                 printf("\nFile %d: ", i+1);
  66.                 ptr = ll[i];
  67.                 while(ptr != NULL)
  68.                 {
  69.                         printf("%d ", ptr -> mloc);
  70.                         ptr = ptr -> next;
  71.                 }
  72.         }
  73.         printf("\n");
  74. }
  75.  
  76.  
  77.  
  78. OUTPUT
  79.  
  80. Enter the number of files: 4
  81. Enter the number of blocks for each file:
  82. File 1: 3
  83. File 2: 2
  84. File 3: 4
  85. File 4: 12
  86.  
  87. DISK:
  88.  
  89.     0   1   2   3   4   5   6   7   8   9
  90.  
  91. 0   1   0   1   1   1   1   0   0   1   1  
  92. 10  0   1   0   1   1   0   0   0   0   0  
  93. 20  1   0   1   1   0   0   0   1   1   1  
  94. 30  1   0   0   0   1   1   1   0   1   0  
  95. 40  1   1   1   1   0   1   0   0   1   0  
  96. 50  1   0   1   0   0   1   0   0   0   1  
  97. 60  1   1   0   1   0   1   0   1   1   1  
  98. 70  0   1   0   1   0   1   0   0   1   0  
  99. 80  1   0   0   0   0   0   1   1   0   1  
  100. 90  0   0   0   0   1   0   0   0   0   1  
  101.  
  102.  
  103. Linked lists are:
  104. File 1: 1 6 7
  105. File 2: 10 12
  106. File 3: 15 16 17 18
  107. File 4: 19 21 24 25 26 31 32 33 37 39 44 46
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement