diff --git a/a_station.c b/a_station.c new file mode 100644 index 0000000..bf194bb --- /dev/null +++ b/a_station.c @@ -0,0 +1,96 @@ +#include "a_station.h" +#include +#include + +struct station* create_station(){ + struct station* station = calloc(1,sizeof(struct station)); + station->tracks = calloc(STATION_SIZE, sizeof(struct car*)); + station->capacity = STATION_SIZE; + return station; +} + +void destroy_station(struct station* station){ + for (int i = 0; i < station->capacity; i++){ + /// Jedna polozka je spojkovy zoznam, uvolni cely spojkovy zoznam + struct car* list = station->tracks[i]; + while (list != NULL){ + struct car* del = list; + list = del->next; + free(del); + } + } + free(station->tracks); + free(station); +} + +int select_track(struct station* station, const char* target){ + unsigned int hash = 0; + for (int counter = 0; target[counter]!='\0'; counter++){ + hash = target[counter] + (hash << 6) + (hash << 16) - hash; + } + + if(hash < 0 || hash > station->capacity){ + hash = hash % station->capacity; + } + return hash; +} + +void add_target_capacity(struct station* station,const char* target, int capacity){ + int slot_index = select_track(station,target); + + struct car* slot = station->tracks[slot_index]; + while (slot != NULL){ + if(strcmp(slot->value,target) == 0){ + //strcpy(slot->value, target); + slot->capacity += capacity; + return; + } + slot = slot->next; + } + struct car* new = malloc(sizeof(struct car)); + strcpy(new->value,target); + new->capacity = capacity; + new->next = slot; + station->tracks[slot_index] = new; + if (new->next){ + return; + } + return; + +} + +int get_target_capacity(struct station* station,const char* target){ + int slot_index = select_track(station, target); + struct car* car = station->tracks[slot_index]; + while(car != NULL){ + if(strcmp(car->value,target)== 0){ + return car->capacity; + } + car = car->next; + } + return -1; +} + +int count_targets(struct station* station){ + int num_of_targets = 0; + for(int i = 0; i < STATION_SIZE; i++){ + if(station->tracks[i] != NULL) num_of_targets++; + } + return num_of_targets; +} + +int count_capacity(struct station* station){ + int sum_capacity = 0; + + for(int i = 0; i < STATION_SIZE; i++){ + struct car* train = station->tracks[i]; + while(train != NULL){ + sum_capacity += train->capacity; + train = train->next; + } + } + + return sum_capacity; + +} + diff --git a/hashtable b/hashtable new file mode 100755 index 0000000..0430c56 Binary files /dev/null and b/hashtable differ