-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked.c
184 lines (168 loc) · 3.52 KB
/
linked.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#define maxsize 100
struct block{
int value;
struct block *next;
};
struct fileEntry{
char *name;
int start;
int length;
};
struct block disk[maxsize];
int freeSpace = maxsize;
struct fileEntry files[30];
void init(){
int i;
for (i = 0; i < 30; i++){
files[i].name = NULL; //No files present
}
for (i = 0; i < maxsize; i++){
disk[i].value = 0; //Disk is Empty & blocks do not point to any other block
disk[i].next = NULL;
}
}
int getEmptySlot(){
int i;
for (i = 0; i < 30; i++){
if (files[i].name == NULL)
return i;
}
return -1;
}
void insertFile(char *name, int blocks){
if (blocks > freeSpace){
printf("\nFile size too big\n");
return;
}
if (searchFile(name) != -1){
printf("\nCannot insert the file\n");
return;
}
int start = -1;
int i, j, allocated = 0;
int prev = -1;
for (i = 0; i<maxsize; i++){
if (disk[i].value == 0){
if (start == -1){
start = i;
}
disk[i].value = 1;
if(prev != -1)
disk[prev].next = &disk[i];
allocated++;
prev = i;
}
if (allocated == blocks){
disk[i].next = NULL;
break;
}
}
int slot = getEmptySlot();
files[slot].name = malloc(0);
strcpy(files[slot].name, name);
files[slot].length = blocks;
files[slot].start = start;
freeSpace -= blocks;
printf("Inserted\n");
}
void deleteFile(char *name){
int pos;
int size = 0;
if ((pos = searchFile(name)) == -1){
printf("\nFile not found\n");
return;
}
disk[files[pos].start].value = 0;
struct block *temp = disk[files[pos].start].next;
while (temp != NULL){
temp->value = 0;
size++;
temp = temp->next;
}
freeSpace += size+1;
files[pos].name = NULL;
printf("Deleted\n");
}
int searchFile(char *name){
int i;
for (i = 0; i<30; i++){
if (files[i].name != NULL && strcmp(files[i].name, name) == 0)
return i;
}
return -1;
}
void displaySize(){
printf("Free space in disk = %d", freeSpace);
}
void displayDisk(){
int i;
printf("\nDISK:\n\n\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\n");
for (i = 0; i<maxsize; i++){
if (i % 10 == 0)
printf("\n%d\t", i);
printf("%d\t", disk[i].value);
}
printf("\n");
}
void displayFiles(){
int i;
printf("Files in disk:\n");
printf("Name\tStart\tLength\n\n");
for (i = 0; i < 30; i++){
if (files[i].name != NULL){
printf("%s\t%4d\t%3d\n", files[i].name, files[i].start, files[i].length);
printf("Blocks: %d -> ", files[i].start);
struct block *temp = disk[files[i].start].next;
while (temp != NULL){
printf("%d -> ", (temp-disk));
temp = temp->next;
}
printf("NULL\n");
}
}
printf("\n");
}
int main(){
int option;
char* name = (char*)malloc(20 * sizeof(char));
int blocks, start;
init();
printf("Linked File allocation technique\n\n");
printf("\n1. Insert a File");
printf("\n2. Delete a File");
printf("\n3. Display the disk");
printf("\n4. Display all files");
printf("\n5. Exit\n");
while (1){
displaySize();
printf("\nEnter option: ");
scanf_s("%d", &option);
switch (option){
case 1:
printf("Enter file name: ");
getchar();
gets_s(name, 20);
printf("Enter number of blocks: ");
scanf_s("%d", &blocks);
insertFile(name, blocks);
break;
case 2:
printf("Enter file name to delete: ");
getchar();
gets_s(name, 20);
deleteFile(name);
break;
case 3:
displayDisk();
break;
case 4:
displayFiles();
break;
case 5:
exit(1);
default:
exit(1);
}
}
}