-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.c
903 lines (762 loc) · 31.8 KB
/
Code.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
Done By: Saif Al-Deen Shakoor Battah.
Sec#: 2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//Define Passenger & Bus Linked Lists and Pointers
typedef struct Pass_Node *Pass_ptr;
typedef Pass_ptr Pass_List;
typedef struct Bus_Node *Bus_ptr;
typedef Bus_ptr Bus_List;
//Define Passenger node as initialization of the creation of passenger linked list
struct Pass_Node
{
int Pass_ID;
int Pass_Date;
char Pass_Time[30];
char Pass_From[30];
char Pass_To[30];
Bus_ptr AddedBus;
Pass_ptr nextPass;
};
//Define Bus node as initialization of the creation of Buses linked list
struct Bus_Node
{
int Bus_Number;
int Bus_Date;
char Bus_Time[30];
char Bus_From[30];
char Bus_To[30];
int Price;
int Capacity;
Pass_ptr FirstPass; //first passenger in the bus
Bus_ptr nextBus;
};
//Prototype of some functions
Pass_ptr FindPreviousPass(Pass_List PL, int ID);
Pass_ptr Delete_Pass(Pass_List PL, int ID, int x);
Bus_ptr Delete_Bus(Bus_List BL, int Bus_No);
Bus_ptr FindPreviousBus(Bus_List BL, int Bus_No);
Bus_ptr Scaning(Bus_List BL, int Number);
int main()
{
// Creation of Bus List
Bus_List BL;
BL = (Bus_List)malloc(sizeof(struct Bus_Node));
BL->nextBus = NULL;
//---------------------
// Creation of Passengers List
Pass_List PL;
PL = (Pass_List)malloc(sizeof(struct Pass_Node));
PL->nextPass = NULL;
//---------------------
// Creation of a temp list to store matched passengers together for a while
Pass_List MatchedList;
MatchedList=(Pass_List)malloc(sizeof(struct Pass_Node));
MatchedList->nextPass = NULL;
//---------------------
// Creation of list to store unmatched passengers.
Pass_List UnmatchedList;
UnmatchedList = (Pass_List)malloc(sizeof(struct Pass_Node));
UnmatchedList->nextPass = NULL;
//---------------------
int Choice;
int Firsty=0;
int Secondy=0;
// temp list's used to assign passengers to their correct buses
Bus_ptr temp_bus_search;
temp_bus_search = (Bus_ptr)malloc(sizeof(struct Bus_Node));
Pass_ptr temp_pass_search;
temp_pass_search = (Pass_ptr)malloc(sizeof(struct Pass_Node));
Pass_ptr Update_Pass_List;
Update_Pass_List = (Pass_ptr)malloc(sizeof(struct Pass_Node));
Pass_ptr um_pass;
um_pass = (Pass_ptr)malloc(sizeof(struct Pass_Node));
Pass_ptr m_pass;
m_pass = (Pass_ptr)malloc(sizeof(struct Pass_Node));
Bus_ptr m_bus;
m_bus = (Bus_ptr)malloc(sizeof(struct Bus_Node));
// list used to store passengers in a deleted bus to append to the unmatched list
Bus_ptr Temp_UM;
Temp_UM = (Bus_ptr)malloc(sizeof(struct Bus_Node));
char str1[30];
char str2[30];
int counts;
int ID;
int Date;
char Time[30];
char From[30];
char To[30];
int Update = 0;
printf("Menu\n\n1. Load the bus information file.\n2. Load the passenger information file.\n3. Assign passengers and print assignment information of all busses.\n4. Print a specific bus information along with its passengers information (names and IDs).\n5. Print unmatched passengers.\n6. Add new passenger.\n7. Delete passenger.\n8. Delete bus number.\n9. Exit.\n");
printf("\nEnter choice: ");
scanf("%d",&Choice);
while(Choice!= 9 )
{
if(Choice == 1)
{
Load_Bus_List(BL); //Loading bus information from bus file and creating a bus list
Firsty++;
}
else if(Choice == 2)
{
Load_Pass_List(PL); //Loading passenger information from passenger file and creating a passenger list
Secondy++;
}
else if(Choice == 3)
{
if(Firsty == 0 && Secondy == 0)
{
printf("\nPlease Load Bus & Passengers List!\n\n"); //Error message if not bus list neither passenger list are loaded
}
else if(Firsty > 0 && Secondy == 0)
{
printf("\nPlease Load Passengers List!\n\n"); //Error message if passenger list only not loaded
}
else if(Firsty == 0 && Secondy >0)
{
printf("\nPlease Load Bus List\n\n"); //Error message if bus list only not loaded
}
else //If both lists are loaded then go to assign process
{
if(!isEmptyBus(BL) && !isEmptyPass(PL))
{ // check if linked lists are empty or not !
if(Update == 1) // if any deleting process occur get in here
{
Clear_List(BL); // clear bus list
PL = Update_Pass_List; // clear passenger list
}
temp_pass_search = PL; // define Bus Linked list as temp LL for searching purpose
while(temp_pass_search->nextPass != NULL)
{ // node by node until reaching last node
temp_bus_search = BL; // define Passengers Linked list as temp LL for searching purpose
counts = 0;
while(temp_bus_search->nextBus != NULL)
{ // node by node until reaching last node
//Matching Search!
strcpy(str1,temp_bus_search->nextBus->Bus_Time);
strcpy(str2,temp_pass_search->nextPass->Pass_Time);
if(temp_bus_search->nextBus->Bus_Date == temp_pass_search->nextPass->Pass_Date && !strcmp(str1,str2))
{
///printf("\nMatch Found!\n------------\n\nBus Date = %d Passenger Date = %d |",temp_bus_search->nextBus->Bus_Date,temp_pass_search->nextPass->Pass_Date);
///printf(" Bus Time = %s Passenger Time = %s\n\n------------\n",temp_bus_search->nextBus->Bus_Time,temp_pass_search->nextPass->Pass_Time);
// initialize passenger information
ID = temp_pass_search->nextPass->Pass_ID;
Date = temp_pass_search->nextPass->Pass_Date;
strcpy(Time,temp_pass_search->nextPass->Pass_Time);
strcpy(From,temp_pass_search->nextPass->Pass_From);
strcpy(To,temp_pass_search->nextPass->Pass_To);
counts++;
break;
}
else
{
temp_bus_search = temp_bus_search->nextBus;
}
//End of Matching search!
}
if(counts>0) // Add passenger to matching bus
{
m_pass = temp_pass_search->nextPass;
m_bus = temp_bus_search->nextBus;
///printf("\n=============================Start\n");
///printf("\nMatched Passenger!\nPassenger Added to Bus %d\n",m_bus->Bus_Number);
///printf("\nID=%d\nDate=%d\nTime=%s\nFrom=%s\nTo=%s\n",m_pass->Pass_ID,m_pass->Pass_Date,m_pass->Pass_Time,m_pass->Pass_From,m_pass->Pass_To);
MatchedList->nextPass = m_bus;
Add_Pass(&MatchedList,m_pass->Pass_ID,m_pass->Pass_Date,m_pass->Pass_Time,m_pass->Pass_From,m_pass->Pass_To);
///printf("\n=============================End\n\n");
}
else
{ // add passenger to unmatching list
um_pass = temp_pass_search->nextPass;
///printf("\n=============================Start\n");
///printf("\nUnmatched Passenger!\nPassenger Added to Unmatched List!!\n");
///printf("\nID=%d\nDate=%d\nTime=%s\nFrom=%s\nTo=%s\n",um_pass->Pass_ID,um_pass->Pass_Date,um_pass->Pass_Time,um_pass->Pass_From,um_pass->Pass_To);
Add_Pass(&UnmatchedList,um_pass->Pass_ID,um_pass->Pass_Date,um_pass->Pass_Time,um_pass->Pass_From,um_pass->Pass_To);
///printf("\n=============================Start\n");
}
temp_pass_search = temp_pass_search->nextPass; // go to next bus node to search within it
}
}
else
{
printf("Link List is Empty!!!");
}
Printing(BL); //Print each bus information with its passenger information
printf("Assigning Completed Successfully!\n\n");
}
}
else if(Choice == 4)
{
int Bus_No;
printf("\nEnter Bus Number To Search For:\n");
scanf("%d",&Bus_No);
Searching(BL,Bus_No); // a function to search for the bus selected
}
else if(Choice == 5)
{
if(UnmatchedList->nextPass != NULL)
{
printf("\nUnmatched Passenger List:\n-------------------------\n\n");
Print_Pass_List(UnmatchedList); //print unmatched passengers list
printf("\n");
}
else
{
printf("\nTheres No Unmatched Passengers!\n\n"); //if theres no passenger in the unmatched list
}
}
else if(Choice == 6)
{
//ask user to enter new passenger information
printf("\nAdding New Passenger!\n---------------------\n\n");
printf("ID = ");
scanf("%d",&ID);
printf("\nDate(In Form of DayMonthYear Ex: 15072022) = ");
scanf("%d",&Date);
printf("\nTime((In Form of Hour:Minute Ex: 16:30)) = ");
scanf("%s",&Time);
printf("\nFrom = ");
scanf("%s",&From);
printf("\nTo = ");
scanf("%s",&To);
printf("\n");
//create passenger node and add it to the passenger list
Add_Pass(&PL,ID,Date,Time,From,To);
//print the passenger list after adding
Print_Pass_List(PL);
}
else if(Choice == 7)
{
if(PL->nextPass != NULL)
{
//ask user to enter passenger id to search if exist then delete it
printf("\n");
Print_Pass_List(PL);
int ID;
printf("\n\nEnter Passenger ID to Delete:\n");
scanf("%d",&ID);
PL = Delete_Pass(PL,ID,1); // 1 used to whole passenger list , so we get a error message if passenger doesn't exist or list is empty
printf("\n");
Print_Pass_List(PL);
UnmatchedList = Delete_Pass(UnmatchedList,ID,0); // delete passenger from unmatched list if exist, 0 is used to skip the not found or null list error message
Update = 1;
Update_Pass_List = PL; //load passenger list to temporary list for assign process
printf("\n");
}
else
{//if the list is null
printf("\nNo Passengers To remove!\n");
}
}
else if(Choice == 8)
{
if(BL->nextBus != NULL)
{
//ask user to enter bus number to search for it and delete it
printf("\n");
Print_Bus_List(BL);
int Bus_No;
printf("\nEnter Bus Number To Delete\n");
scanf("%d",&Bus_No);
Temp_UM = Scaning(BL,Bus_No);
if(Temp_UM != NULL && Temp_UM->FirstPass != NULL)
{//if bus already deleted , append its passenger to unmatched list
Add_To_Unmatch(UnmatchedList,Temp_UM);
}
BL = Delete_Bus(BL,Bus_No);
printf("\n");
Print_Bus_List(BL);
printf("\n");
}
else//if bus list is empty
{
printf("\nNo Buses To Remove!\n");
}
}
else
{//Error message if user enter a wrong input
printf("\nPlease Enter a number from 1 to 9!\n");
}
printf("\n\nMenu\n\n1. Load the bus information file.\n2. Load the passenger information file.\n3. Assign passengers and print assignment information of all busses.\n4. Print a specific bus information along with its passengers information (names and IDs).\n5. Print unmatched passengers.\n6. Add new passenger.\n7. Delete passenger.\n8. Delete bus number.\n9. Exit.\n");
printf("\nEnter choice: ");
scanf("%d",&Choice);
}
if(Choice == 9)
{//Exit statement
printf("\nBye\n");
}
return 0;
}
void Load_Bus_List(Bus_List BL)
{
FILE *Bus_File;
char str[100];
char line[100];
char *split;
//Bus inputs
int Bus_Number;
int Bus_Date;
char Bus_Time[30];
char Bus_From[30];
char Bus_To[30];
int Price;
int Capacity;
Bus_File = fopen("busses.txt","r");
//statement to check if file exist or not
if(Bus_File == NULL)
{
printf("\nFile Doesn't Exists\n\n");
}
else
{
printf("\nFile Successfully Opened!\n\n");
//read line by line from file
while(fgets(str,sizeof(str),Bus_File) != NULL)
{
strcpy(line,str);
split = strtok(line,"#");
Bus_Number = atoi(split);
split = strtok(NULL,"#");
Bus_Date = atoi(split);
split = strtok(NULL,"#");
strcpy(Bus_Time, split);
split = strtok(NULL,"#");
strcpy(Bus_From,split);
split = strtok(NULL,"#");
strcpy(Bus_To,split);
split = strtok(NULL,"#");
Price = atoi(split);
split = strtok(NULL,"#");
Capacity = atoi(split);
//create new node of the information read form file and add node to list
Add_Bus(&BL,Bus_Number,Bus_Date,Bus_Time,Bus_From,Bus_To,Price,Capacity);
}
//end of reading
}
fclose(Bus_File);
Print_Bus_List(BL);
printf("\n====================================\nBus Information Loaded Successfully!\n====================================\n\n");
}
void Add_Bus(Bus_List *head_bus, int Number, int Date, char Time[], char From[], char To[], int price, int capacity)
{
//statement to check if bus already exist or not in the list
if(Search_Bus(*head_bus,Number) == 0)
{//if new bus
Bus_ptr newBusNode = (Bus_ptr)malloc(sizeof(struct Bus_Node));
newBusNode->FirstPass = NULL;
Bus_ptr last = *head_bus;
newBusNode->Bus_Number = Number;
newBusNode->Bus_Date = Date;
strcpy(newBusNode->Bus_Time, Time);
strcpy(newBusNode->Bus_From,From);
strcpy(newBusNode->Bus_To,To);
newBusNode->Price = price;
newBusNode->Capacity = capacity;
newBusNode->nextBus = NULL;
if(*head_bus == NULL)
{
*head_bus = newBusNode;
return;
}
while(last->nextBus != NULL)
{
last = last->nextBus;
}
last->nextBus=newBusNode;
return;
}
else
{//if bus already exist
printf("\nBus Already Exist!\n\n");
}
}
void Print_Bus_List(Bus_List BL)
{
//statement to check if bus list empty or not
if(!isEmptyBus(BL))
{//if list not NULL
Bus_ptr temp_bus = BL->nextBus;
while(temp_bus != NULL)
{//while loop to print bus by bus
printf("| %d | %d | %s | %s | %s | %d | %d\n",temp_bus->Bus_Number,temp_bus->Bus_Date,temp_bus->Bus_Time,temp_bus->Bus_From,temp_bus->Bus_To,temp_bus->Price,temp_bus->Capacity);
temp_bus = temp_bus->nextBus;
if(temp_bus != NULL)
{
printf("------>");
}
}
}
else
{//if list is null
printf("\nEmpty Linked List!\n");
}
}
int isEmptyBus(Bus_List BL)
{//function to check if bus list is empty or not
if(BL == NULL)
{
return 1; // null bus
}
else
{
return BL->nextBus == NULL; // return next node position
}
}
void Load_Pass_List(Pass_List PL)
{
FILE *Pass_File;
char str[100];
char line[100];
char *split;
//Passenger inputs
int Pass_ID;
int Pass_Date;
char Pass_Time[30];
char Pass_From[30];
char Pass_To[30];
Pass_File = fopen("passengers.txt","r");
//statement to check if passenger file exist or not
if(Pass_File == NULL)
{
printf("\nFile Doesn't Exists\n\n");
}
else
{
printf("\nFile Successfully Opened!\n\n");
//read line by line from file
while(fgets(str,sizeof(str),Pass_File) != NULL)
{
strcpy(line,str);
split = strtok(line,"#");
Pass_ID = atoi(split);
split = strtok(NULL,"#");
Pass_Date = atoi(split);
split = strtok(NULL,"#");
strcpy(Pass_Time, split);
split = strtok(NULL,"#");
strcpy(Pass_From,split);
split = strtok(NULL,"#");
strcpy(Pass_To,split);
//create node of read information from passenger file and add node to passenger list
Add_Pass(&PL,Pass_ID,Pass_Date,Pass_Time,Pass_From,Pass_To);
}
//end of reading
}
fclose(Pass_File);
Print_Pass_List(PL);
printf("\n\n===========================================\nPassengers Information Loaded Successfully!\n===========================================\n\n");
}
void Add_Pass(Pass_List *head_pass, int ID, int Date, char Time[], char From[], char To[])
{//statement to check if passenger is unique or not
if(Search_Pass(*head_pass,ID) == 0)
{
Pass_ptr newPassNode = (Pass_ptr)malloc(sizeof(struct Pass_Node));
Pass_ptr last = *head_pass;
newPassNode->Pass_ID = ID;
newPassNode->Pass_Date = Date;
strcpy(newPassNode->Pass_Time, Time);
strcpy(newPassNode->Pass_From,From);
strcpy(newPassNode->Pass_To,To);
newPassNode->nextPass = NULL;
if(*head_pass == NULL)
{
*head_pass = newPassNode;
return;
}
while(last->nextPass != NULL)
{
last = last->nextPass;
}
last->nextPass=newPassNode;
return;
}
else
{
printf("\nPassenger Already Exists!\n\n");
}
}
void Print_Pass_List(Pass_List PL)
{//print passenger by passenger from passenger list
if(!isEmptyPass(PL))
{
Pass_ptr temp_pass = PL->nextPass;
while(temp_pass != NULL)
{
printf("| %d | %d | %s | %s | %s ",temp_pass->Pass_ID,temp_pass->Pass_Date,temp_pass->Pass_Time,temp_pass->Pass_From,temp_pass->Pass_To);
temp_pass = temp_pass->nextPass;
if(temp_pass != NULL)
{
printf("------->");
}
}
}
else
{
printf("Empty Passengers List!!\n\n");
}
}
int isEmptyPass(Pass_List PL)
{//check if passenger list is empty or not
if(PL == NULL)
{
return 1;
}
else
{
return PL->nextPass == NULL;
}
}
void Printing(Bus_List BL)
{// function to read bus by bus and then print each bus with its own passenger information
printf("\n");
printf("\n=====================================================\n");
Pass_ptr temp;
if(!isEmptyBus(BL))//check if bus list is empty or not
{
Bus_ptr temp_bus = BL->nextBus;
while(temp_bus != NULL)
{//while bus list not finished, read its own passenger list and print node by node
printf("| %d | %d | %s | %s | %s | %d | %d",temp_bus->Bus_Number,temp_bus->Bus_Date,temp_bus->Bus_Time,temp_bus->Bus_From,temp_bus->Bus_To,temp_bus->Price,temp_bus->Capacity);
temp = temp_bus->FirstPass;
if(temp != NULL)
{
printf(" :\n");
printf("->");
Printing_Pass_List(temp);
}
temp_bus = temp_bus->nextBus;
if(temp_bus != NULL)
{
printf("\n=====================================================\n");
}
}
printf("\n=====================================================\n");
}
else
{//Error message if bus list is empty
printf("\nEmpty Bus List!\n");
}
printf("\n");
}
void Printing_Pass_List(Pass_List PL)
{//read passenger list node by node and print each node by itself
while(PL != NULL)
{
printf("| %d | %d | %s | %s | %s",PL->Pass_ID,PL->Pass_Date,PL->Pass_Time,PL->Pass_From,PL->Pass_To);
if(PL->nextPass != NULL)
{
printf("-->");
}
PL = PL->nextPass;
}
}
void Searching(Bus_List BL, int Number)
{//function to search for a specific bus based on its number, and print this bus details with its passenger details
BL = BL->nextBus;
while(BL != NULL)
{
if(BL->Bus_Number == Number)
{
printf("\n--------------------\nBus %d Found Already!\n--------------------\n\n",BL->Bus_Number);
printf("Bus Information:\n| %d | %d | %s | %s | %s | %d | %d |\n",BL->Bus_Number,BL->Bus_Date,BL->Bus_Time,BL->Bus_From,BL->Bus_To,BL->Price,BL->Capacity);
if(BL->FirstPass != NULL)
{
printf("\n\nPassengers Information:\n",BL->Bus_Number);
}
Printing_Pass_List(BL->FirstPass);
printf("\n");
break;
}
BL=BL->nextBus;
}
if(BL == NULL)
{
printf("\n-------------------\nBus Doesn't Exist!!\n-------------------\n");
}
}
Pass_ptr Delete_Pass(Pass_List PL, int ID, int x)
{//delete passenger and return the new passenger list
if(!isEmptyPass(PL))
{
if(Search_Pass(PL,ID) == 1)
{
Pass_ptr ptr = FindPreviousPass(PL,ID);
if(ptr != NULL)
{
Pass_ptr deleted_pass = ptr->nextPass;
ptr->nextPass = deleted_pass->nextPass;
free(deleted_pass);
printf("\n----------------------------------------\nPassenger Has Been Deleted Successfully!\n----------------------------------------\n");
}
}
else
{
if(x == 1)
{
printf("\n------------------------\nPassenger Doesn'n Exist!\n------------------------\n");
}
}
}
else
{
if(x == 1)
{
printf("\nYou Can't Delete From an Empty List!\n");
}
}
return PL;
}
Pass_ptr FindPreviousPass(Pass_List PL, int ID)
{//used to redirect the pointer of previous node and let it point to the next after deleting process
Pass_ptr a = PL->nextPass;
Pass_ptr b = PL;
while(a != NULL && b->nextPass->Pass_ID != ID)
{
b = b->nextPass;
a = a->nextPass;
}
if(a == NULL)
{
return a;
}
else
{
return b;
}
}
int Search_Pass(Pass_List PL, int ID)
{//function to search for a specific passenger using his id number
PL = PL->nextPass;
while(PL != NULL)
{
if(PL->Pass_ID == ID)
{
return 1;
break;
}
PL = PL->nextPass;
}
if(PL == NULL)
{
return 0;
}
}
void Deleteing(Pass_List PL, int ID)
{
if(!isEmptyPass(PL))
{
if(Search_Pass(PL,ID) == 1)
{
Pass_ptr ptr = FindPreviousPass(PL,ID);
if(ptr != NULL)
{
Pass_ptr deleted_pass = ptr->nextPass;
ptr->nextPass = deleted_pass->nextPass;
free(deleted_pass);
}
}
}
}
void Clear_List(Bus_List BL)
{//clear list after deleting bus or passenger so no misleading occur
Bus_ptr TBL = BL->nextBus;
while(TBL != NULL)
{
if(TBL->FirstPass != NULL)
{
while(TBL->FirstPass != NULL)
{
Deleteing(TBL->FirstPass,TBL->FirstPass->Pass_ID);
TBL->FirstPass = TBL->FirstPass->nextPass;
}
}
TBL = TBL->nextBus;
}
}
Bus_ptr Delete_Bus(Bus_List BL, int Bus_No)
{//function to delete bus using his number
if(!isEmptyBus(BL))
{
if(Search_Bus(BL,Bus_No) == 1)
{
Bus_ptr flag = FindPreviousBus(BL,Bus_No);
if(flag != NULL)
{
Bus_ptr d = flag->nextBus;
flag->nextBus = d->nextBus;
free(d);
}
printf("\n-----------------------------\nBus Has Deleted Successfully!\n-----------------------------\n");
}
else
{
printf("\n------------------\nBus Doesn't Exist!\n------------------\n");
}
}
else
{
printf("\nYou Can't Delete From an Empty List!\n");
}
return BL;
}
Bus_ptr FindPreviousBus(Bus_List BL, int Bus_No)
{//redirect pointer from previous to next and excepting the deleted node
Bus_ptr a = BL->nextBus;
Bus_ptr b = BL;
while(a != NULL && b->nextBus->Bus_Number != Bus_No)
{
b = b->nextBus;
a = a->nextBus;
}
if(a == NULL)
{
return a;
}
else
{
return b;
}
}
int Search_Bus(Bus_List BL, int Number)
{//search in bus list for a specific bus using his number
BL = BL->nextBus;
while(BL != NULL)
{
if(BL->Bus_Number == Number)
{
return 1;
break;
}
BL = BL->nextBus;
}
if(BL == NULL)
{
return 0;
}
}
void Add_To_Unmatch(Pass_List UnmatchedList, Bus_ptr BL)
{//add passengers from a deleted bus to unmatched list
Pass_ptr TempPass = BL->FirstPass;
while(TempPass != NULL)
{
Add_Pass(&UnmatchedList,TempPass->Pass_ID,TempPass->Pass_Date,TempPass->Pass_Time,TempPass->Pass_From,TempPass->Pass_To);
TempPass = TempPass->nextPass;
}
}
Bus_ptr Scaning(Bus_List BL, int Number)
{//function that search for a specific bus using his number and return a pointer to that bus, so we can use this pointer to add this bus passengers to unmatched list before deleting it!
BL = BL->nextBus;
while(BL != NULL)
{
if(BL->Bus_Number == Number)
{
return BL;
break;
}
BL = BL->nextBus;
}
}