Untitled

lab project _1 code
 avatar
unknown
plain_text
a month ago
6.4 kB
1
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Structure for patient details (BST Node)
typedef struct Patient {
    int id;
    char name[50];
    int age;
    char disease[50];
    struct Patient *left, *right;
} Patient;

// Structure for Queue (Patient Appointment Management)
typedef struct QueueNode {
    int id;
    char name[50];
    struct QueueNode *next;
} QueueNode;

// Structure for Stack (Medical History)
typedef struct StackNode {
    int id;
    char history[100];
    struct StackNode *next;
} StackNode;

Patient *root = NULL;
QueueNode *front = NULL, *rear = NULL;
StackNode *top = NULL;

// Function to insert patient record in BST
Patient* insertPatient(Patient *root, int id, char name[], int age, char disease[]) {
    if (root == NULL) {
        Patient *newNode = (Patient*)malloc(sizeof(Patient));
        newNode->id = id;
        strcpy(newNode->name, name);
        newNode->age = age;
        strcpy(newNode->disease, disease);
        newNode->left = newNode->right = NULL;
        return newNode;
    }
    if (id < root->id)
        root->left = insertPatient(root->left, id, name, age, disease);
    else
        root->right = insertPatient(root->right, id, name, age, disease);
    return root;
}

// Function to search patient record in BST
void searchPatient(Patient *root, int id) {
    if (root == NULL) {
        printf("Patient not found!\n");
        return;
    }
    if (id == root->id) {
        printf("\n--- Patient Found ---\nID: %d\nName: %s\nAge: %d\nDisease: %s\n", root->id, root->name, root->age, root->disease);
        return;
    }
    if (id < root->id)
        searchPatient(root->left, id);
    else
        searchPatient(root->right, id);
}

// Function to enqueue patient (Queue - Appointment Management)
void enqueue(int id, char name[]) {
    QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode));
    newNode->id = id;
    strcpy(newNode->name, name);
    newNode->next = NULL;
    if (rear == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
    printf("Patient %s added to appointment queue.\n", name);
}

// Function to dequeue patient (Queue - Process Appointment)
void dequeue() {
    if (front == NULL) {
        printf("No patients in queue.\n");
        return;
    }
    QueueNode *temp = front;
    printf("\nProcessing Appointment for: %s\n", temp->name);
    front = front->next;
    if (front == NULL) rear = NULL;
    free(temp);
}

// Function to push medical history (Stack - LIFO)
void pushHistory(int id, char history[]) {
    StackNode *newNode = (StackNode*)malloc(sizeof(StackNode));
    newNode->id = id;
    strcpy(newNode->history, history);
    newNode->next = top;
    top = newNode;
    printf("Medical history added for Patient ID %d.\n", id);
}

// Function to pop medical history (Stack - LIFO)
void popHistory() {
    if (top == NULL) {
        printf("No medical history available.\n");
        return;
    }
    StackNode *temp = top;
    printf("\n--- Latest Medical History ---\nPatient ID: %d\nHistory: %s\n", temp->id, temp->history);
    top = top->next;
    free(temp);
}

// Function to free memory of BST
void freeBST(Patient *root) {
    if (root == NULL) return;
    freeBST(root->left);
    freeBST(root->right);
    free(root);
}

// Function to free queue memory
void freeQueue() {
    while (front) {
        QueueNode *temp = front;
        front = front->next;
        free(temp);
    }
}

// Function to free stack memory
void freeStack() {
    while (top) {
        StackNode *temp = top;
        top = top->next;
        free(temp);
    }
}

// Main Function
int main() {
    int choice, id, age;
    char name[50], disease[50], history[100];

    while (1) {
        printf("\n--- Hospital Management System ---\n");
        printf("1. Register Patient\n");
        printf("2. Search Patient\n");
        printf("3. Add to Appointment Queue\n");
        printf("4. Process Appointment\n");
        printf("5. Add Medical History\n");
        printf("6. View Latest Medical History\n");
        printf("7. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // Clear buffer

        switch (choice) {
            case 1:
                printf("Enter Patient ID: ");
                scanf("%d", &id);
                getchar();
                printf("Enter Name: ");
                fgets(name, 50, stdin);
                strtok(name, "\n"); // Remove newline
                printf("Enter Age: ");
                scanf("%d", &age);
                getchar();
                printf("Enter Disease: ");
                fgets(disease, 50, stdin);
                strtok(disease, "\n"); // Remove newline

                root = insertPatient(root, id, name, age, disease);
                printf("Patient Registered Successfully!\n");
                break;

            case 2:
                printf("Enter Patient ID to search: ");
                scanf("%d", &id);
                searchPatient(root, id);
                break;

            case 3:
                printf("Enter Patient ID: ");
                scanf("%d", &id);
                getchar();
                printf("Enter Name: ");
                fgets(name, 50, stdin);
                strtok(name, "\n");
                enqueue(id, name);
                break;

            case 4:
                dequeue();
                break;

            case 5:
                printf("Enter Patient ID: ");
                scanf("%d", &id);
                getchar();
                printf("Enter Medical History: ");
                fgets(history, 100, stdin);
                strtok(history, "\n"); // Remove newline
                pushHistory(id, history);
                break;

            case 6:
                popHistory();
                break;

            case 7:
                printf("Exiting and freeing memory...\n");
                freeBST(root);
                freeQueue();
                freeStack();
                exit(0);

            default:
                printf("Invalid Choice! Try Again.\n");
        }
    }
    return 0;
}
Editor is loading...
Leave a Comment