#include <iostream>
#include <vector>
#include <iomanip>
class FileSystem {
public:
FileSystem(int totalBlocks) : blocks(totalBlocks, false) {}
// Contiguous Allocation
bool allocateContiguous(int fileSize, int& startBlock) {
for (int i = 0; i <= blocks.size() - fileSize; ++i) {
bool canAllocate = true;
for (int j = i; j < i + fileSize; ++j) {
if (blocks[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
startBlock = i;
for (int j = i; j < i + fileSize; ++j) {
blocks[j] = true;
}
return true;
}
}
return false;
}
// Linked Allocation
bool allocateLinked(const std::vector<int>& fileBlocks, std::vector<int>& fileTable) {
fileTable.clear();
for (int block : fileBlocks) {
if (block >= blocks.size() || blocks[block]) {
return false;
}
blocks[block] = true;
fileTable.push_back(block);
}
return true;
}
// Indexed Allocation
bool allocateIndexed(const std::vector<int>& fileBlocks, std::vector<int>& indexTable) {
indexTable.clear();
for (int block : fileBlocks) {
if (block >= blocks.size() || blocks[block]) {
return false;
}
blocks[block] = true;
indexTable.push_back(block);
}
return true;
}
void printBlocks() const {
std::cout << "Block Status:\n";
for (int i = 0; i < blocks.size(); ++i) {
std::cout << "Block " << i << ": " << (blocks[i] ? "Allocated" : "Free") << "\n";
}
}
private:
std::vector<bool> blocks;
};
int main() {
int totalBlocks;
std::cout << "Enter total number of blocks in the file system: ";
std::cin >> totalBlocks;
FileSystem fs(totalBlocks);
int choice;
std::cout << "Select allocation method:\n";
std::cout << "1. Contiguous Allocation\n";
std::cout << "2. Linked Allocation\n";
std::cout << "3. Indexed Allocation\n";
std::cout << "Enter choice (1-3): ";
std::cin >> choice;
if (choice == 1) {
int fileSize, startBlock;
std::cout << "Enter file size for contiguous allocation: ";
std::cin >> fileSize;
if (fs.allocateContiguous(fileSize, startBlock)) {
std::cout << "File allocated starting at block " << startBlock << "\n";
} else {
std::cout << "Allocation failed. Not enough contiguous space.\n";
}
} else if (choice == 2) {
int numBlocks;
std::cout << "Enter number of blocks for linked allocation: ";
std::cin >> numBlocks;
std::vector<int> fileBlocks(numBlocks);
std::cout << "Enter blocks for linked allocation: ";
for (int& block : fileBlocks) {
std::cin >> block;
}
std::vector<int> fileTable;
if (fs.allocateLinked(fileBlocks, fileTable)) {
std::cout << "File allocated with linked allocation.\n";
std::cout << "File Table: ";
for (int block : fileTable) {
std::cout << block << " ";
}
std::cout << "\n";
} else {
std::cout << "Allocation failed. Invalid blocks or already allocated.\n";
}
} else if (choice == 3) {
int numBlocks;
std::cout << "Enter number of blocks for indexed allocation: ";
std::cin >> numBlocks;
std::vector<int> fileBlocks(numBlocks);
std::cout << "Enter blocks for indexed allocation: ";
for (int& block : fileBlocks) {
std::cin >> block;
}
std::vector<int> indexTable;
if (fs.allocateIndexed(fileBlocks, indexTable)) {
std::cout << "File allocated with indexed allocation.\n";
std::cout << "Index Table: ";
for (int block : indexTable) {
std::cout << block << " ";
}
std::cout << "\n";
} else {
std::cout << "Allocation failed. Invalid blocks or already allocated.\n";
}
} else {
std::cout << "Invalid choice.\n";
}
fs.printBlocks();
return 0;
}