This commit is contained in:
Filip Chochol 2026-04-15 15:50:36 +02:00
parent dd425613b2
commit 876df59231

View File

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
struct station* create_station(){ struct station* create_station(){
struct station* stanica = calloc(1, sizeof(struct station)); struct station* stanica = calloc(1, sizeof(struct station));
stanica->tracks = calloc(STATION_SIZE, sizeof(struct car*)); stanica->tracks = calloc(STATION_SIZE, sizeof(struct car*));
stanica->track_count = STATION_SIZE; stanica->track_count = STATION_SIZE;
@ -10,6 +11,7 @@ struct station* create_station(){
} }
void destroy_station(struct station* stanica){ void destroy_station(struct station* stanica){
for (int i = 0; i < stanica->track_count; i++){ for (int i = 0; i < stanica->track_count; i++){
struct car* aktualny = stanica->tracks[i]; struct car* aktualny = stanica->tracks[i];
while (aktualny != NULL){ while (aktualny != NULL){
@ -20,18 +22,22 @@ void destroy_station(struct station* stanica){
} }
free(stanica->tracks); free(stanica->tracks);
free(stanica); free(stanica);
} }
int select_track(struct station* stanica, const char* ciel){ int select_track(struct station* stanica, const char* ciel){
unsigned long heslo = 5381; unsigned long heslo = 5381;
int znak; int znak;
while ((znak = *ciel++)){ while ((znak = *ciel++)){
heslo = ((heslo << 5) + heslo) + znak; heslo = ((heslo << 5) + heslo) + znak;
} }
return heslo % stanica->track_count; return heslo % stanica->track_count;
} }
void add_target_capacity(struct station* stanica, const char* ciel, int kapacita){ void add_target_capacity(struct station* stanica, const char* ciel, int kapacita){
int index = select_track(stanica, ciel); int index = select_track(stanica, ciel);
struct car* aktualny = stanica->tracks[index]; struct car* aktualny = stanica->tracks[index];
@ -48,9 +54,11 @@ void add_target_capacity(struct station* stanica, const char* ciel, int kapacita
novy->capacity = kapacita; novy->capacity = kapacita;
novy->next = stanica->tracks[index]; novy->next = stanica->tracks[index];
stanica->tracks[index] = novy; stanica->tracks[index] = novy;
} }
int get_target_capacity(struct station* stanica, const char* ciel){ int get_target_capacity(struct station* stanica, const char* ciel){
int index = select_track(stanica, ciel); int index = select_track(stanica, ciel);
struct car* aktualny = stanica->tracks[index]; struct car* aktualny = stanica->tracks[index];
@ -62,9 +70,11 @@ int get_target_capacity(struct station* stanica, const char* ciel){
} }
return 0; return 0;
} }
int count_targets(struct station* stanica){ int count_targets(struct station* stanica){
int pocet = 0; int pocet = 0;
for (int i = 0; i < stanica->track_count; i++){ for (int i = 0; i < stanica->track_count; i++){
@ -76,9 +86,11 @@ int count_targets(struct station* stanica){
} }
return pocet; return pocet;
} }
int count_capacity(struct station* stanica){ int count_capacity(struct station* stanica){
int celkovo = 0; int celkovo = 0;
for (int i = 0; i < stanica->track_count; i++){ for (int i = 0; i < stanica->track_count; i++){
@ -90,4 +102,5 @@ int count_capacity(struct station* stanica){
} }
return celkovo; return celkovo;
} }