Fixing segfault

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

View File

@ -28,56 +28,34 @@ 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 res = select_track(station, target);
int count = 0; struct car* previous = NULL;
int res = select_track(station, target); struct car** ptr = &station->tracks[res];
while(ptr){ struct car* head = *ptr;
if(count == res){
break; while(head){
} if(strcmp(head->value, target) == 0){
if (strcmp(ptr->value, target) ==0 ){ head->capacity += capacity;
ptr->capacity += capacity;
return; return;
} }
previous = head;
ptr = ptr->next; head = head->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(previous){
if(ptr){ previous->next = next;
ptr->next = next;
return;}
else{
*station->tracks = next;
return;
}
} }
else if (count == res && ptr && strcmp(ptr->value, target) != 0){ else{
struct car* next = (struct car*)calloc(1, sizeof(struct car)); *ptr = next;
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;
@ -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;
} }