diff --git a/du6/station.c b/du6/station.c new file mode 100644 index 0000000..608db60 --- /dev/null +++ b/du6/station.c @@ -0,0 +1,147 @@ +#include "station.h" + +#include +#include + +struct station* create_station() { + struct station* st = (struct station*)malloc(sizeof(struct station)); + if (st == NULL) { + return NULL; + } + + st->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + if (st->tracks == NULL) { + free(st); + return NULL; + } + + st->track_count = STATION_SIZE; + return st; +} + +void destroy_station(struct station* station) { + int i; + + if (station == NULL) { + return; + } + + for (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 long hash = 5381; + const unsigned char* ptr; + + if (station == NULL || station->track_count <= 0 || target == NULL) { + return 0; + } + + ptr = (const unsigned char*)target; + while (*ptr != '\0') { + hash = ((hash << 5) + hash) + *ptr; + ptr++; + } + + return (int)(hash % (unsigned long)station->track_count); +} + +void add_target_capacity(struct station* station, const char* target, int capacity) { + int index; + struct car* current; + + if (station == NULL || station->tracks == NULL || target == NULL) { + return; + } + + index = select_track(station, target); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + current->capacity += capacity; + return; + } + current = current->next; + } + + current = (struct car*)malloc(sizeof(struct car)); + if (current == NULL) { + return; + } + + strncpy(current->value, target, TARGET_SIZE - 1); + current->value[TARGET_SIZE - 1] = '\0'; + current->capacity = capacity; + current->next = station->tracks[index]; + station->tracks[index] = current; +} + +int get_target_capacity(struct station* station, const char* target) { + int index; + struct car* current; + + if (station == NULL || station->tracks == NULL || target == NULL) { + return 0; + } + + index = select_track(station, target); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + return current->capacity; + } + current = current->next; + } + + return 0; +} + +int count_targets(struct station* station) { + int i; + int count = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (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 i; + int sum = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + sum += current->capacity; + current = current->next; + } + } + + return sum; +}