0% found this document useful (0 votes)
3 views4 pages

Question

The document contains two C++ programs: one for managing a file system using a tree structure and another for a patient management system using a linked list. The file system program allows adding files and folders, and printing the structure, while the patient management program includes functionalities for inserting, deleting, and searching patient records by disease. Both programs demonstrate fundamental data structure operations in C++.

Uploaded by

m.tashfeenchohan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Question

The document contains two C++ programs: one for managing a file system using a tree structure and another for a patient management system using a linked list. The file system program allows adding files and folders, and printing the structure, while the patient management program includes functionalities for inserting, deleting, and searching patient records by disease. Both programs demonstrate fundamental data structure operations in C++.

Uploaded by

m.tashfeenchohan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question # 02:

Program:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class FileSystemNode {
public:
string name;
vector<FileSystemNode*> children;

FileSystemNode(string nodeName) {
name = nodeName;
}
};

FileSystemNode* findFolder(FileSystemNode* root, const string& targetName) {


if (root == nullptr) return nullptr;
if (root->name == targetName) return root;

for (FileSystemNode* child : root->children) {


FileSystemNode* found = findFolder(child, targetName);
if (found != nullptr) return found;
}
return nullptr;
}

void addFileOrFolder(FileSystemNode* root, const string& parentName, const string& newName) {


FileSystemNode* parent = findFolder(root, parentName);
if (parent != nullptr) {
parent->children.push_back(new FileSystemNode(newName));
}
else {
cout << "Error: Parent folder '" << parentName << "' not found.\n";
}
}

void printFileSystem(FileSystemNode* root, int depth = 0) {


if (root == nullptr) return;

for (int i = 0; i < depth; ++i) {


cout << " ";
}
cout << "|-- " << root->name << "\n";

for (FileSystemNode* child : root->children) {


printFileSystem(child, depth + 1);
}
}
int main() {
FileSystemNode* fileSystemRoot = new FileSystemNode("Root");

addFileOrFolder(fileSystemRoot, "Root", "Docs");


addFileOrFolder(fileSystemRoot, "Root", "Media");
addFileOrFolder(fileSystemRoot, "Root", "Projects");

addFileOrFolder(fileSystemRoot, "Docs", "[Link]");


addFileOrFolder(fileSystemRoot, "Docs", "[Link]");

addFileOrFolder(fileSystemRoot, "Media", "song.mp3");


addFileOrFolder(fileSystemRoot, "Media", "movie.mp4");

addFileOrFolder(fileSystemRoot, "Projects", "project1");


addFileOrFolder(fileSystemRoot, "project1", "[Link]");

addFileOrFolder(fileSystemRoot, "Projects", "project2");


addFileOrFolder(fileSystemRoot, "project2", "[Link]");

cout << "--- File System Layout ---\n";


printFileSystem(fileSystemRoot);

return 0;
}

OutPut:

Question # 03:

Patient Management System

1. Node Structure Design

Program:
#include <iostream>
#include <string>

using namespace std;

struct PatientNode {
int patientID;
string patientName;
int age;
string disease;
string admissionDate;
PatientNode* next;

PatientNode(int id, string name, int patientAge, string patientDisease, string date) {
patientID = id;
patientName = name;
age = patientAge;
disease = patientDisease;
admissionDate = date;
next = nullptr;
}
};

2. Insert a New Patient Record at the End

void insertPatient(PatientNode*& head, int id, string name, int age, string disease,
string date) {
PatientNode* newNode = new PatientNode(id, name, age, disease, date);

if (head == nullptr) {
head = newNode;
return;
}

PatientNode* temp = head;


while (temp->next != nullptr) {
temp = temp->next;
}

temp->next = newNode;
}

3. Delete a Patient Record by Patient ID

void dischargePatient(PatientNode*& head, int targetID) {


if (head == nullptr) {
cout << "No patients currently admitted.\n";
return;
}
if (head->patientID == targetID) {
PatientNode* nodeToDelete = head;
head = head->next;
delete nodeToDelete;
cout << "Patient " << targetID << " discharged successfully.\n";
return;
}

PatientNode* current = head;


PatientNode* previous = nullptr;

while (current != nullptr && current->patientID != targetID) {


previous = current;
current = current->next;
}

if (current == nullptr) {
cout << "Error: Patient ID " << targetID << " not found.\n";
return;
}

previous->next = current->next;
delete current;
cout << "Patient " << targetID << " discharged successfully.\n";
}
4. Search and Display Patients by Disease

void searchByDisease(PatientNode* head, string targetDisease) {


bool found = false;
PatientNode* temp = head;

cout << "--- Patients with " << targetDisease << " ---\n";

while (temp != nullptr) {


if (temp->disease == targetDisease) {
cout << "ID: " << temp->patientID
<< " | Name: " << temp->patientName
<< " | Age: " << temp->age
<< " | Admitted: " << temp->admissionDate << "\n";
found = true;
}
temp = temp->next;
}

if (!found) {
cout << "No patients currently admitted with this disease.\n";
}
}

You might also like