From c1d12b35086d8b3da5da327faafecb7b25e99e26 Mon Sep 17 00:00:00 2001 From: Oleksandr Lytvyn Date: Tue, 17 Dec 2019 21:59:23 +0100 Subject: [PATCH] code for exam 2019 --- a_station.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hashtable | Bin 0 -> 12792 bytes 2 files changed, 96 insertions(+) create mode 100644 a_station.c create mode 100755 hashtable 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 0000000000000000000000000000000000000000..0430c56fead27cc7d25268931ace2a2f4f08fd1e GIT binary patch literal 12792 zcmeHNeQ;FO6~CKJAZpkw$VUW}Cj})cOHe+7O3fxLyg1=Q2-=Ru$CBNUZ8y7_-M4MR zh-DhF-9{*#@sCWWondOnQrkaJ$Cf&dBu(X`)0xIjt4`Gxt#&qm16zicI zB@3#_3e!#%#|{ci7EK-rCc7ib?ufEunpJ@@<^ECM=-;gTI_Wyds4yj6DqB7@LsW0C zkn98+vS7iK?H+)g8ZxEv--%a=x2pE8l$RZx7e!Tf#jCbsx*Ilamn~6Rl?y4K37#;LCn|sV@Yd(_;qWiN=c_*uY#GW1FOx0* zQN8Ly?anBoy$zUF1iubgG5xA5;480y$AN43JIew9#r!`GTyrXezXkt=MR0+9w^Djn ziO!Unq(f_aFd;e02p`#^#MiD4YoIiX_c&qIqLOG#(2#2X{uva#u$@rYsFhwWy*sswEg(8eRU= z*wGK`i0VsfZsZ!0Z%{m+cYMBhr&@x;>ilASNKNW;O_K_YGym2C3nw+6U!-{2hTGSR zj19M+FIgMTZIGW)8;%a-%Ta|>*wjxh{Wd(mlt6gWhVwZ~HbXXCr9isUd(M^Xy=8&9 zn$Y+4n>-t$3rOLNoyvWrNczJFSeBx}nNd~VjWuo2-03!7<$@=q|CT|DdYp%SniTi1{I+yc zeHr%pz-70d&gui1`*hcF{q@Uc6&(7N!wPbEC4YO@HbPCLmgzlf=TL1s#Z#g8uAK`m zGYwtN=r|lNyBWNT!r4oKeKrV}=ntv447WjpYUw>=y&K0~-qqIDmOlBfGXodh>5~Ja z3#xyc9vV1Z;-W+KK>GLTp_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