-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHospitalRecordManagementSystem.c
More file actions
398 lines (375 loc) · 9.11 KB
/
Copy pathHospitalRecordManagementSystem.c
File metadata and controls
398 lines (375 loc) · 9.11 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//Author:Harshada Avhad
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// Function to clear the input buffer
void clearInputBuffer() {
while (getchar() != '\n');
}
// Structures
typedef struct Pdata {
char name[50], disease[50];
int age, id;
} Pdata;
typedef struct Patient {
Pdata data;
struct Patient *next;
} Patient;
typedef struct Doctor {
char name[50];
Patient* patient;
struct Doctor* next;
} Doctor;
typedef struct Hospital {
char name[50];
Doctor* d;
struct Hospital* next;
} Hospital;
// Init Functions
Patient* initP(int id, char* name, int age) {
Patient* newP = (Patient*) malloc(sizeof(Patient));
newP->data.id = id;
strcpy(newP->data.name, name);
newP->data.age = age;
newP->next = NULL;
return newP;
}
Doctor* initD(const char* name) {
Doctor* newD = (Doctor*) malloc(sizeof(Doctor));
strcpy(newD->name, name);
newD->patient = NULL;
newD->next = NULL;
return newD;
}
Hospital* initH(const char* name) {
Hospital* newH = (Hospital*) malloc(sizeof(Hospital));
strcpy(newH->name, name);
newH->d = NULL;
newH->next = NULL;
return newH;
}
// Memory Cleanup
void freePatient(Patient* patient) {
free(patient);
}
void freeDoctor(Doctor* doctor) {
Patient* temp = doctor->patient;
while (temp != NULL) {
Patient* next = temp->next;
freePatient(temp);
temp = next;
}
free(doctor);
}
void freeHospital(Hospital* hospital) {
Doctor* temp = hospital->d;
while (temp != NULL) {
Doctor* next = temp->next;
freeDoctor(temp);
temp = next;
}
free(hospital);
}
// Add Functions
void addPtoD(Doctor *doctor, Patient *newp) {
if (doctor->patient == NULL) {
doctor->patient = newp;
} else {
Patient *temp = doctor->patient;
while (temp->next != NULL)
temp = temp->next;
temp->next = newp;
}
}
void addDtoH(Hospital* hospital, Doctor* newD) {
if (hospital->d == NULL) {
hospital->d = newD;
} else {
Doctor* temp = hospital->d;
while (temp->next != NULL)
temp = temp->next;
temp->next = newD;
}
}
void addH(Hospital** Hlist, Hospital* newH) {
if (*Hlist == NULL) {
*Hlist = newH;
} else {
Hospital* temp = *Hlist;
while (temp->next != NULL)
temp = temp->next;
temp->next = newH;
}
}
// Print Functions
void pPofD(Doctor *doctor) {
Patient *temp = doctor->patient;
while (temp != NULL) {
printf("Patient ID: %d, Name: %s, Age: %d\n", temp->data.id, temp->data.name, temp->data.age);
temp = temp->next;
}
}
void pDofH(Hospital* hospital) {
Doctor* temp = hospital->d;
while (temp != NULL) {
printf("Doctor: %s\n", temp->name);
pPofD(temp);
temp = temp->next;
}
}
void pH(Hospital** Hlist) {
Hospital* temp = *Hlist;
if (temp == NULL) {
printf("No Hospitals.\n");
} else {
while (temp != NULL) {
printf("Hospital: %s\n", temp->name);
temp = temp->next;
}
}
}
// Patient Operations
void editP(Doctor* doctor) {
int id;
printf("Enter the patient ID to edit: ");
scanf("%d", &id);
Patient* temp = doctor->patient;
while (temp != NULL && temp->data.id != id)
temp = temp->next;
if (temp != NULL) {
printf("Editing patient %d\n", id);
printf("Enter new name: ");
scanf(" %49[^\n]", temp->data.name);
printf("Enter new age: ");
scanf("%d", &temp->data.age);
printf("Patient updated successfully!\n");
} else {
printf("Patient not found.\n");
}
}
void deleteP(Doctor* doctor) {
int id;
printf("Enter the patient ID to delete: ");
scanf("%d", &id);
Patient* temp = doctor->patient;
Patient* prev = NULL;
while (temp != NULL && temp->data.id != id) {
prev = temp;
temp = temp->next;
}
if (temp != NULL) {
if (prev == NULL) {
doctor->patient = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
printf("Patient %d deleted successfully!\n", id);
} else {
printf("Patient not found.\n");
}
}
void searchPatientByName(Doctor* doctor) {
char name[50];
printf("Enter the patient's name to search: ");
scanf(" %49[^\n]", name);
Patient* temp = doctor->patient;
int found = 0;
while (temp != NULL) {
if (strcmp(temp->data.name, name) == 0) {
printf("Patient found: ID: %d, Name: %s, Age: %d\n",
temp->data.id, temp->data.name, temp->data.age);
found = 1;
}
temp = temp->next;
}
if (!found) {
printf("No patient with that name found.\n");
}
}
void viewPatientInfo(Doctor* doctor) {
int id;
printf("Enter the patient ID to view: ");
scanf("%d", &id);
Patient* temp = doctor->patient;
while (temp != NULL && temp->data.id != id)
temp = temp->next;
if (temp != NULL) {
printf("Patient ID: %d\nName: %s\nAge: %d\n",
temp->data.id, temp->data.name, temp->data.age);
} else {
printf("Patient not found.\n");
}
}
// Menu
void menu() {
printf("\n===== Hospital Patient Record System =====\n");
printf("1. Add new Hospital\n");
printf("2. Add new Doctor to Hospital\n");
printf("3. Add new Patient to Doctor\n");
printf("4. View List of Hospitals\n");
printf("5. Select Hospital\n");
printf("6. View Doctors in Hospital\n");
printf("7. Select Doctor\n");
printf("8. View Waiting List of Patients under Doctor\n");
printf("9. View Patients under Doctor\n");
printf("10. View Patient Information by ID\n");
printf("11. Edit Patient Information\n");
printf("12. Delete Patient\n");
printf("13. Search Patient by Name\n");
printf("14. Exit\n");
printf("==========================================\n");
}
// Main
int main() {
Hospital* Hlist = NULL;
int choice;
char name[50];
int id, age;
Patient* patient;
Doctor* doctor = NULL;
Hospital* hospital = NULL;
while (1) {
menu();
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1) {
clearInputBuffer();
printf("Invalid input. Please enter a number.\n");
continue;
}
clearInputBuffer();
switch (choice) {
case 1:
printf("Enter hospital name: ");
scanf(" %49[^\n]", name);
hospital = initH(name);
addH(&Hlist, hospital);
printf("Hospital %s added.\n", name);
break;
case 2:
if (Hlist == NULL) {
printf("No hospitals available.\n");
break;
}
printf("Enter hospital name to add doctor: ");
scanf(" %49[^\n]", name);
hospital = Hlist;
while (hospital != NULL && strcmp(hospital->name, name) != 0)
hospital = hospital->next;
if (hospital == NULL) {
printf("Hospital not found.\n");
break;
}
printf("Enter doctor name: ");
scanf(" %49[^\n]", name);
addDtoH(hospital, initD(name));
printf("Doctor added to %s.\n", hospital->name);
break;
case 3:
if (hospital == NULL || hospital->d == NULL) {
printf("Select a hospital and ensure it has doctors.\n");
break;
}
printf("Enter doctor name: ");
scanf(" %49[^\n]", name);
doctor = hospital->d;
while (doctor != NULL && strcmp(doctor->name, name) != 0)
doctor = doctor->next;
if (doctor == NULL) {
printf("Doctor not found.\n");
break;
}
printf("Enter patient ID: ");
scanf("%d", &id);
printf("Enter patient name: ");
scanf(" %49[^\n]", name);
printf("Enter patient age: ");
scanf("%d", &age);
addPtoD(doctor, initP(id, name, age));
printf("Patient added to Dr. %s.\n", doctor->name);
break;
case 4:
pH(&Hlist);
break;
case 5:
printf("Enter hospital name to select: ");
scanf(" %49[^\n]", name);
hospital = Hlist;
while (hospital != NULL && strcmp(hospital->name, name) != 0)
hospital = hospital->next;
if (hospital)
printf("Hospital %s selected.\n", hospital->name);
else
printf("Hospital not found.\n");
break;
case 6:
if (!hospital) {
printf("Select a hospital first.\n");
break;
}
pDofH(hospital);
break;
case 7:
if (!hospital) {
printf("Select a hospital first.\n");
break;
}
printf("Enter doctor name to select: ");
scanf(" %49[^\n]", name);
doctor = hospital->d;
while (doctor != NULL && strcmp(doctor->name, name) != 0)
doctor = doctor->next;
if (doctor)
printf("Doctor %s selected.\n", doctor->name);
else
printf("Doctor not found.\n");
break;
case 8:
case 9:
if (!doctor) {
printf("Select a doctor first.\n");
break;
}
pPofD(doctor);
break;
case 10:
if (!doctor) {
printf("Select a doctor first.\n");
break;
}
viewPatientInfo(doctor);
break;
case 11:
if (!doctor) {
printf("Select a doctor first.\n");
break;
}
editP(doctor);
break;
case 12:
if (!doctor) {
printf("Select a doctor first.\n");
break;
}
deleteP(doctor);
break;
case 13:
if (!doctor) {
printf("Select a doctor first.\n");
break;
}
searchPatientByName(doctor);
break;
case 14:
printf("Exiting...\n");
while (Hlist != NULL) {
Hospital* next = Hlist->next;
freeHospital(Hlist);
Hlist = next;
}
default:
printf("Invalid choice.\n");
}
}
return 0;
}