#include #include #include #include "a_station.h" static unsigned long vytvor_hash(const char* nazov){ unsigned long h=0;int z; while((z=*nazov++))h=h*31+z; return h; } struct station* create_station(){ struct station* stanica=malloc(sizeof(struct station)); if(!stanica)return NULL; stanica->tracks=calloc(STATION_SIZE,sizeof(struct car*)); if(!stanica->tracks){free(stanica);return NULL;} stanica->track_count=STATION_SIZE; return stanica; } void destroy_station(struct station* stanica){ if(!stanica)return; for(int i=0;itrack_count;i++){ struct car* a=stanica->tracks[i]; while(a){ struct car* d=a->next; free(a); a=d; } } free(stanica->tracks); free(stanica); } int select_track(struct station* stanica,const char* nazov){ if(!stanica||!nazov)return 0; unsigned long h=vytvor_hash(nazov); return (int)(h%stanica->track_count); } void add_target_capacity(struct station* stanica,const char* nazov,int pocet){ if(!stanica||!nazov)return; int cislo=select_track(stanica,nazov); struct car* a=stanica->tracks[cislo]; struct car* p=NULL; while(a){ if(strcmp(a->value,nazov)==0){ a->capacity+=pocet; return; } p=a;a=a->next; } struct car* novy=malloc(sizeof(struct car)); if(!novy)return; strncpy(novy->value,nazov,TARGET_SIZE-1); novy->value[TARGET_SIZE-1]='\0'; novy->capacity=pocet; novy->next=NULL; if(p==NULL)stanica->tracks[cislo]=novy; else p->next=novy; } int get_target_capacity(struct station* stanica,const char* nazov){ if(!stanica||!nazov)return 0; int cislo=select_track(stanica,nazov); struct car* a=stanica->tracks[cislo]; while(a){ if(strcmp(a->value,nazov)==0)return a->capacity; a=a->next; } return 0; } int count_targets(struct station* stanica){ if(!stanica)return 0; int pocet=0; for(int i=0;itrack_count;i++){ struct car* a=stanica->tracks[i]; while(a){pocet++;a=a->next;} } return pocet; } int count_capacity(struct station* stanica){ if(!stanica)return 0; int suma=0; for(int i=0;itrack_count;i++){ struct car* a=stanica->tracks[i]; while(a){suma+=a->capacity;a=a->next;} } return suma; }