#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the node structure
typedef struct node {
int event_id;
char event_name[100];
char date[20];
struct node *left;
struct node *right;
} node;
node *start = NULL;
// Function to create and return a new node
node* getnode() {
node *newnode = (node*) malloc(sizeof(node));
printf("\nEnter Event ID: ");
scanf("%d", &newnode->event_id);
printf("Enter Event Name: ");
scanf(" %[^\n]s", newnode->event_name);
printf("Enter Event Date (DD-MM-YYYY): ");
scanf(" %[^\n]s", newnode->date);
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
// Recursive function to count number of nodes
int countnode(node *start) {
if (start == NULL)
return 0;
else
return (1 + countnode(start->right));
}
// Function to log 'n' events at the end
void createlist(int n) {
int i;
node *newnode, *temp;
for (i = 0; i < n; i++) {
newnode = getnode();
if (start == NULL) {
start = newnode;
} else {
temp = start;
while (temp->right != NULL)
temp = temp->right;
temp->right = newnode;
newnode->left = temp;
}
}
}
// Function to insert an event at the beginning
void insert_at_beginning() {
node *newnode = getnode();
if (start == NULL) {
start = newnode;
} else {
newnode->right = start;
start->left = newnode;
start = newnode;
}
printf("\nNew event inserted at the beginning.\n");
}
// Function to delete the most recent event (from the end)
void delete_at_end() {
node *temp;
if (start == NULL) {
printf("\nEvent log is empty.\n");
return;
}
temp = start;
while (temp->right != NULL)
temp = temp->right;
if (temp->left != NULL)
temp->left->right = NULL;
else
start = NULL; // only one node
free(temp);
printf("\nMost recent event deleted from the log.\n");
}
// Function to display all events
void traverse() {
node *temp = start;
if (start == NULL) {
printf("\nEvent log is empty.\n");
return;
}
printf("\n--- Hospital Event Log (Oldest to Newest) ---\n");
while (temp != NULL) {
printf("Event ID: %d\nEvent Name: %s\nDate: %s\n\n",
temp->event_id, temp->event_name, temp->date);
temp = temp->right;
}
}
// Main function
int main() {
int choice, n;
while (1) {
printf("\n--- Hospital Event Log Menu ---\n");
printf("1. Log 'n' events\n");
printf("2. Insert a new event at the beginning\n");
printf("3. Delete the most recent event (from the end)\n");
printf("4. Display all events\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter number of events to log: ");
scanf("%d", &n);
createlist(n);
break;
case 2:
insert_at_beginning();
break;
case 3:
delete_at_end();
break;
case 4:
traverse();
break;
case 5:
printf("\nExiting...\n");
exit(0);
default:
printf("\nInvalid choice. Try again.\n");
}
}
return 0;
}