Fixing segfault

This commit is contained in:
Anton Dolozin 2025-11-05 11:11:49 +01:00
parent 9f096f6d20
commit 44c9a7a188

View File

@ -28,57 +28,35 @@ int select_track(struct station* station, const char* target){
} }
void add_target_capacity(struct station* station,const char* target, int capacity){ void add_target_capacity(struct station* station,const char* target, int capacity){
struct car* ptr = *station->tracks;
int count = 0;
int res = select_track(station, target); int res = select_track(station, target);
while(ptr){ struct car* previous = NULL;
if(count == res){ struct car** ptr = &station->tracks[res];
break; struct car* head = *ptr;
}
if (strcmp(ptr->value, target) ==0 ){ while(head){
ptr->capacity += capacity; if(strcmp(head->value, target) == 0){
head->capacity += capacity;
return; return;
} }
previous = head;
head = head->next;
ptr = ptr->next;
count++;
} }
if(count < res){
struct car* next = (struct car*)calloc(1, sizeof(struct car)); struct car* next = (struct car*)calloc(1, sizeof(struct car));
strcpy(next->value, target); strcpy(next->value, target);
next->capacity = capacity; next->capacity = capacity;
if(ptr){ if(previous){
ptr->next = next; previous->next = next;
return;} }
else{ else{
*station->tracks = next; *ptr = next;
return;
} }
}
else if (count == res && ptr && strcmp(ptr->value, target) != 0){
struct car* next = (struct car*)calloc(1, sizeof(struct car));
strcpy(next->value, target);
next->capacity = capacity;
if(ptr){
ptr->next = next;
return;}
else{
*station->tracks = next;
return;
}
}
if (!ptr){
return;
}
ptr->capacity += capacity;
return;
} }
int get_target_capacity(struct station* station,const char* target){ int get_target_capacity(struct station* station,const char* target){
struct car* ptr = *station->tracks; struct car* ptr = *station->tracks;
while (ptr) while (ptr)
@ -109,9 +87,13 @@ int count_targets(struct station* station){
} }
int count_capacity(struct station* station){ int count_capacity(struct station* station){
int total = 0; int res = 0;
for (struct car* ptr = *station->tracks; ptr; ptr = ptr->next) struct car* ptr = *station->tracks;
total += ptr->capacity; while (ptr){
return total; res += ptr->capacity;
ptr = ptr->next;
}
return res;
} }