#include "a_station.h" #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* stanica) { for (int i = 0; i < stanica->track_count; ++i) { struct car* aktualny = stanica->tracks[i]; while (aktualny) { struct car* docasny = aktualny; aktualny = aktualny->next; free(docasny); } } free(stanica->tracks); free(stanica); } int select_track(struct station* stanica, const char* ciel) { unsigned int hash = 0; for (int i = 0; ciel[i] != '\0'; i++) { hash = (hash * 31 + ciel[i]) % stanica->track_count; } return hash; } void add_target_capacity(struct station* stanica, const char* ciel, int kapacita) { int index = select_track(stanica, ciel); struct car* aktualny = stanica->tracks[index]; while (aktualny != NULL) { if (strcmp(aktualny->value, ciel) == 0) { aktualny->capacity += kapacita; return; } aktualny = aktualny->next; } struct car* novy_voz = (struct car*)malloc(sizeof(struct car)); strncpy(novy_voz->value, ciel, TARGET_SIZE - 1); novy_voz->value[TARGET_SIZE - 1] = '\0'; novy_voz->capacity = kapacita; novy_voz->next = stanica->tracks[index]; stanica->tracks[index] = novy_voz; } int get_target_capacity(struct station* stanica, const char* ciel) { int index = select_track(stanica, ciel); struct car* aktualny = stanica->tracks[index]; while (aktualny != NULL) { if (strcmp(aktualny->value, ciel) == 0) { return aktualny->capacity; } aktualny = aktualny->next; } return 0; } int count_targets(struct station* stanica) { int pocet = 0; for (int i = 0; i < stanica->track_count; i++) { struct car* aktualny = stanica->tracks[i]; while (aktualny) { pocet++; aktualny = aktualny->next; } } return pocet; } int count_capacity(struct station* stanica) { int celkova_kapacita = 0; for (int i = 0; i < stanica->track_count; i++) { struct car* aktualny = stanica->tracks[i]; while (aktualny) { celkova_kapacita += aktualny->capacity; aktualny = aktualny->next; } } return celkova_kapacita; }