-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.c
351 lines (326 loc) · 9.24 KB
/
LinkedList.c
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "STD_TYPES.h"
#include "LinkedList.h"
extern node_t *ptr;
extern node_t *patientsListHead;
extern u32 timeListSize;
extern reservationsList *reservedTimesPointer;
extern reservationsList *reservedTimesHead;
u32 CheckID(u32 ID){
u32 foundFlag = 0;
while(ptr != NULL){
if(ID == ptr -> ID){
foundFlag = 1;
break;
}
ptr = ptr -> next;
}
return foundFlag;
}
void AddPatientRecord(){
ptr = patientsListHead;
u32 counter =1;
u32 ID = 0;
node_t *node =NULL;
printf("Please enter Patient ID: ");
scanf("%d",&ID);
if(!CheckID(ID)){
node = (node_t*) malloc(1 * sizeof(node_t));
node -> next = NULL;
node -> ID = ID;
}
else{
printf("\n-----------------------------------------\n");
printf("Patient with the same ID already exists");
printf("\n-----------------------------------------\n");
printf("Enter any key to continue ...");
getch();
return;
}
printf("Please enter patient's name: ");
scanf(" %[^\n]s",&(node -> name));
printf("Please enter patient's gender: ");
scanf(" %[^\n]s",&(node -> gender));
printf("Please enter patient's age: ");
scanf("%d",&(node -> age));
node -> reservedTime = NULL;
if(patientsListHead == NULL){
patientsListHead = node;
}
else{
ptr = patientsListHead;
while(1){
if(ptr -> next == NULL){
ptr -> next = node;
break;
}
/*Move the pointer to the next node*/
ptr = ptr -> next;
}
}
printf("\n-----------------------------------------\n");
printf("Patient record added. Thank you");
printf("\n-----------------------------------------\n");
printf("Enter any key to continue ...");
getch();
}
void EditPatientRecord(){
if(patientsListHead == NULL){
printf("\n---------------------------\n");
printf("Patients' List is empty\n");
printf("---------------------------\n");
printf("Enter any key to continue ...");
getch();
return;
}
ptr = patientsListHead;
u32 ID;
printf("Please enter the patient's ID you want to edit his/her record: ");
scanf("%d",&ID);
if(!CheckID(ID)){
printf("\n-------------------------------------------\n");
printf("\tID not found");
printf("\n-------------------------------------------\n");
printf("Enter any key to continue ...");
getch();
return;
}
else{
node_t* temp = ptr;
u32 foundFlag = 0;
ptr = patientsListHead;
printf("Please enter the new patient's ID: ");
scanf("%d",&ID);
while(ptr != NULL){
if(ptr -> ID == ID && ptr != temp){
printf("\n-----------------------------------------\n");
printf("Another patient with the same ID already exists");
printf("\n-----------------------------------------\n");
foundFlag = 1;
break;
}
ptr = ptr -> next;
}
if(!foundFlag){
temp -> ID = ID;
printf("Please enter the new patient's name: ");
scanf(" %[^\n]s",&(temp -> name));
printf("Please enter the new patient's gender: ");
scanf(" %[^\n]s",&(temp -> gender));
printf("Please enter the new patient's age: ");
scanf("%d",&(temp -> age));
printf("\n-------------------------------------------\n");
printf("Patient with ID %d was updated successfully",ID);
printf("\n-------------------------------------------\n");
}
printf("Enter any key to continue ...");
getch();
}
}
u32 ReservationAvailable(){
u32 ID = 0;
printf("Please enter the patient's ID: ");
scanf("%d",&ID);
if(!CheckID(ID)){
printf("\n-------------------------------------------\n");
printf("\tID not found");
printf("\n-------------------------------------------\n");
return 0;
}
else{
if(ptr -> reservedTime != NULL) {
printf("ID %d has another reservation already\n",ID);
return 0;
}
else{ return 1;}
}
}
void ReserveSlot(){
system("cls");
if(reservedTimesHead == NULL){
printf("\n---------------------------\n");
printf("No available times left\n");
printf("---------------------------\n");
printf("Enter any key to continue ...");
getch();
return;
}
reservedTimesPointer = reservedTimesHead;
ptr = patientsListHead;
u32 counter = 1;
printf("\n*******************************************************\n");
printf("Please choose an available reservation time");
printf("\n*******************************************************\n");
printf("\n-------------------------------------------\n");
while(reservedTimesPointer != NULL){
printf("%d- %s\n", counter, reservedTimesPointer -> time);
reservedTimesPointer = reservedTimesPointer -> next;
counter++;
}
printf("-------------------------------------------\n");
reservedTimesPointer = reservedTimesHead;
counter = 1;
u32 choice = 0;
printf("Your choice: ");
scanf("%d",&choice);
if(choice > timeListSize || choice <=0 ) {
printf("\n************************************\n");
printf("\tIncorrect choice");
printf("\n************************************\n");
}
else if(choice == 1){
if(ReservationAvailable()){
reservedTimesHead = reservedTimesHead -> next;
ptr -> reservedTime = reservedTimesPointer;
timeListSize--;
}
}
else{
reservationsList *temp = reservedTimesPointer;
while(1){
if(choice == counter){
if(ReservationAvailable()){
temp -> next = reservedTimesPointer -> next;
ptr -> reservedTime = reservedTimesPointer;
timeListSize--;
}
break;
}
counter++;
temp = reservedTimesPointer;
reservedTimesPointer = reservedTimesPointer -> next;
}
}
printf("Enter any key to continue ...");
getch();
}
void AddReservationTime(u32 position){
u8 reservationTimes[5][20] = {"2 p.m. -> 2:30 p.m.",
"2:30 p.m. -> 3 p.m.",
"3 p.m. -> 3:30 p.m.",
"4 p.m. -> 4:30 p.m.",
"4:30 p.m. -> 5 p.m."};
reservedTimesPointer = reservedTimesHead;
reservationsList *temp = reservedTimesPointer;
reservationsList *node = (reservationsList*) malloc(1 * sizeof(reservationsList));
u32 stringLength = 0;
while(reservationTimes[position - 1][stringLength] != '\0'){
node -> time[stringLength] = reservationTimes[position - 1][stringLength];
stringLength++;
}
node -> time[stringLength] = '\0';
node -> position = position;
if(reservedTimesHead == NULL || reservedTimesHead -> position > position){
reservedTimesHead = node;
reservedTimesHead -> next = reservedTimesPointer;
}
else{
while(reservedTimesPointer != NULL){
if(reservedTimesPointer -> position > position){
temp -> next = node;
node -> next = reservedTimesPointer;
break;
}
temp = reservedTimesPointer;
reservedTimesPointer = reservedTimesPointer -> next;
}
if(position >= timeListSize + 1){
temp -> next = node;
node -> next = reservedTimesPointer;
}
}
reservedTimesPointer = node;
timeListSize++;
}
u8 DisplayReservation(){
system("cls");
ptr = patientsListHead;
reservedTimesPointer = reservedTimesHead;
if(patientsListHead == NULL){
printf("\n---------------------------\n");
printf("No available patients\n");
printf("---------------------------\n");
printf("Enter any key to continue ...");
getch();
return 0;
}
u32 foundFlag = 0;
printf("\n-----------------------------------------------\n");
while(ptr != NULL){
if(ptr -> reservedTime != NULL){
foundFlag =1;
printf("ID %d has reserved time: %s\n", ptr -> ID, ptr -> reservedTime -> time);
}
ptr = ptr -> next;
}
if(!foundFlag){
printf("No reserved times yet.\n");
printf("-----------------------------------------------\n");
printf("Enter any key to continue ...");
getch();
return 0;
}
printf("-----------------------------------------------\n");
return 1;
}
void CancelReservation(){
if(DisplayReservation()){
ptr = patientsListHead;
reservedTimesPointer = reservedTimesHead;
u32 ID = 0;
printf("Please enter patient's ID to cancel his/her reservation: ");
scanf("%d",&ID);
printf("\n-----------------------------------------------\n");
if(CheckID(ID)){
if(ptr -> reservedTime != NULL){
AddReservationTime(ptr -> reservedTime -> position);
ptr -> reservedTime = NULL;
printf("Reservation cancelled successfully.");
}
else{
printf("No available reservations for the ID: %d",ID);
}
printf("\n-----------------------------------------------\n");
}
else{
printf("\tWrong ID");
printf("\n-----------------------------------------------\n");
}
printf("Enter any key to continue ...");
getch();
}
}
void DisplayPatientRecord(){
u32 ID = 0;
system("cls");
if(patientsListHead == NULL){
printf("\n---------------------------\n");
printf("No available patients\n");
printf("---------------------------\n");
printf("Enter any key to continue ...");
getch();
return;
}
printf("\n-----------------------------------------------\n");
printf("\tView Patients");
printf("\n-----------------------------------------------\n");
printf("Please enter patient's ID to display: ");
scanf("%d",&ID);
ptr = patientsListHead;
if(CheckID(ID)){
printf("\n---------------ID %d-----------------\n",ID);
printf("Patient's name is: %s\n",ptr -> name);
printf("Patient's gender is: %s\n",ptr -> gender);
printf("Patient's age is: %d",ptr -> age);
printf("\n------------------------------------\n",ID);
}
else{
printf("\n-----------------------------------------------\n");
printf("\tWrong ID");
printf("\n-----------------------------------------------\n");
}
printf("Enter any key to continue ...");
getch();
}