#include "a_station.h" #include #include struct station* create_station(){ struct station* station = (struct station*)calloc(1,sizeof(struct station)); if (station == NULL) return NULL; station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); if (station->tracks == NULL) return free(station), NULL; station->track_count = STATION_SIZE; return station; } void destroy_station(struct station* station){ if (station == NULL) return; for (int i = 0; i < station->track_count; i++) { struct car* current = station->tracks[i]; while (current != NULL) { struct car* next = current->next; free(current); current = next; } } free(station->tracks); free(station); } int select_track(struct station* station, const char* target){ unsigned int hash = 0; if (station == NULL || target == NULL) return 0; while (*target != '\0') { hash = hash * 31u + (unsigned char)*target; target++; } return (int)(hash % (unsigned int)station->track_count); } void add_target_capacity(struct station* station,const char* target, int capacity){ if (station == NULL || target == NULL) return; int index = select_track(station, target); struct car* current = station->tracks[index]; while (current != NULL) { if (strcmp(current->value, target) == 0) { current->capacity += capacity; return; } current = current->next; } struct car* node = (struct car*)calloc(1, sizeof(struct car)); if (node == NULL) return; strncpy(node->value, target, TARGET_SIZE - 1); node->capacity = capacity; node->next = station->tracks[index]; station->tracks[index] = node; } int get_target_capacity(struct station* station,const char* target){ if (station == NULL || target == NULL) return 0; struct car* current = station->tracks[select_track(station, target)]; while (current != NULL) { if (strcmp(current->value, target) == 0) return current->capacity; current = current->next; } return 0; } int count_targets(struct station* station){ int count = 0; if (station == NULL) return 0; for (int i = 0; i < station->track_count; i++) { struct car* current = station->tracks[i]; while (current != NULL) { count++; current = current->next; } } return count; } int count_capacity(struct station* station){ int sum = 0; if (station == NULL) return 0; for (int i = 0; i < station->track_count; i++) { struct car* current = station->tracks[i]; while (current != NULL) { sum += current->capacity; current = current->next; } } return sum; }