#include "a_station.h" #include #include #include struct station* create_station(){ struct station* station = (struct station*)calloc(1,sizeof(struct station)); station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); station->track_count = STATION_SIZE; return station; } void destroy_station(struct station* station){ free(station->tracks); free(station); } int select_track(struct station* station, const char* target){ unsigned int hash = 0; for (int i = 0; i < strlen(target); i++) { hash += target[i] + (hash<<6) + (hash<<16) - hash; } hash = hash %station->track_count; return hash; } void add_target_capacity(struct station* station,const char* target, int capacity){ int res = select_track(station, target); struct car* previous = NULL; struct car** ptr = &station->tracks[res]; struct car* head = *ptr; while(head){ if(strcmp(head->value, target) == 0){ head->capacity += capacity; return; } previous = head; head = head->next; } struct car* next = (struct car*)calloc(1, sizeof(struct car)); strcpy(next->value, target); next->capacity = capacity; if(previous){ previous->next = next; } else{ *ptr = next; } } int get_target_capacity(struct station* station,const char* target){ struct car* ptr = *station->tracks; while (ptr) { if(strcmp(ptr->value, target) == 0){ return ptr->capacity; } ptr = ptr->next; } return 0; } int count_targets(struct station* station){ int count = 0; if(*station->tracks == NULL || station == NULL){ return 0; } struct car* ptr = *station->tracks; while (ptr) { count++; ptr = ptr->next; } return count; } int count_capacity(struct station* station){ int res = 0; struct car* ptr = *station->tracks; while (ptr){ res += ptr->capacity; ptr = ptr->next; } return res; }