A small FAT file system

A FAT file system must also allocate: when a file is created it takes free blocks, and when it is deleted the blocks go back. The blocks of one file need not be next to each other, because the table links them.

What to build

Run create, delete and show commands on a FAT with n blocks.

Requirements

  • The blocks are numbered 1 to n and all are free at the start. fat[b] is 0 for a free block, -1 for the last block of a file, otherwise the next block of the file.
  • create name k: if a file with this name exists print exists. Else if fewer than k blocks are free print no space. Else the file takes the k lowest-numbered free blocks, chained in increasing order, and you print name: b1 b2 ... bk.
  • delete name: print missing if there is no such file; otherwise free all its blocks and print deleted name (k blocks). show name: print name: b1 b2 ... or missing.
  • After all commands print free blocks: F.

Input

The first line has n (1 ≤ n ≤ 30) and q (1 ≤ q ≤ 40). Then q commands, one per line: create name k (1 ≤ k ≤ 30), delete name or show name. Names are 1 to 8 lower-case letters; at most 20 files exist at once.

Output

One line for each command, then free blocks: F.

Constraints

A deleted name can be created again.

Example

Input:

8 6
create a 3
create b 2
show a
delete a
create c 4
show c

Output:

a: 1 2 3
b: 4 5
a: 1 2 3
deleted a (3 blocks)
c: 1 2 3 6
c: 1 2 3 6
free blocks: 2

Why it works

Because the table links the blocks, a file can use any free blocks, so a long file fits even when the free space is scattered: there is no external fragmentation. Deleting only has to zero the entries; the data blocks themselves are not touched.

Examples

Example 1 · Input
8 6
create a 3
create b 2
show a
delete a
create c 4
show c
Expected output
a: 1 2 3
b: 4 5
a: 1 2 3
deleted a (3 blocks)
c: 1 2 3 6
c: 1 2 3 6
free blocks: 2
Example 2 · Input
3 3
create x 2
create x 1
create y 2
Expected output
x: 1 2
exists
no space
free blocks: 1

Hints

Hint 1

Keep fat[b] for blocks 1 to n (0 free, -1 end of file) and, for each file, its name, start block and size.

Hint 2

create: count the free blocks first. If there are not enough, print no space and change nothing. Otherwise take free blocks from the lowest number up and link them.

Hint 3

delete: walk the chain and set every entry to 0. Remember to remove the name from your list of files.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>

int main(void) {
    int n, q, k, fat[32] = {0};
    int start[20], used[20] = {0};
    char names[20][16];
    char cmd[8], name[16];
    scanf("%d %d", &n, &q);
    for (int i = 0; i < q; i++) {
        scanf("%7s %15s", cmd, name);
        int f = -1;
        for (int j = 0; j < 20; j++)
            if (used[j] && strcmp(names[j], name) == 0) f = j;
        if (strcmp(cmd, "create") == 0) {
            scanf("%d", &k);
            int free_count = 0;
            for (int b = 1; b <= n; b++)
                if (fat[b] == 0) free_count++;
            if (f >= 0) {
                printf("exists\n");
            } else if (free_count < k) {
                printf("no space\n");
            } else {
                int slot = 0;
                while (used[slot]) slot++;
                used[slot] = 1;
                strcpy(names[slot], name);
                int prev = 0;
                printf("%s:", name);
                for (int b = 1; b <= n && k > 0; b++) {
                    if (fat[b] != 0) continue;
                    fat[b] = -1;
                    if (prev == 0) start[slot] = b;
                    else fat[prev] = b;
                    prev = b;
                    k--;
                    printf(" %d", b);
                }
                printf("\n");
            }
        } else if (f < 0) {
            printf("missing\n");
        } else if (strcmp(cmd, "show") == 0) {
            printf("%s:", name);
            for (int b = start[f]; b != -1; b = fat[b]) printf(" %d", b);
            printf("\n");
        } else {
            int count = 0, b = start[f];
            while (b != -1) {
                int next = fat[b];
                fat[b] = 0;
                count++;
                b = next;
            }
            used[f] = 0;
            printf("deleted %s (%d blocks)\n", name, count);
        }
    }
    int free_blocks = 0;
    for (int b = 1; b <= n; b++)
        if (fat[b] == 0) free_blocks++;
    printf("free blocks: %d\n", free_blocks);
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: File systems →