diff --git a/du5/a_station.c b/du5/a_station.c index 4c919ff..c85139e 100644 --- a/du5/a_station.c +++ b/du5/a_station.c @@ -3,87 +3,91 @@ #include #include "a_station.h" -static unsigned long hash_string(const char* t){ - unsigned long h=0;int c; - while((c=*t++)){h=h*31+c;} +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* s=malloc(sizeof(struct station)); - if(s==NULL)return NULL; - s->tracks=calloc(STATION_SIZE,sizeof(struct car*)); - if(s->tracks==NULL){free(s);return NULL;} - s->track_count=STATION_SIZE; - return s; + 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* s){ - if(s==NULL)return; - for(int i=0;itrack_count;i++){ - struct car* c=s->tracks[i]; - while(c!=NULL){ - struct car* n=c->next; - free(c); - c=n; +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(s->tracks); - free(s); + free(stanica->tracks); + free(stanica); } -int select_track(struct station* s,const char* t){ - if(s==NULL||t==NULL)return 0; - unsigned long h=hash_string(t); - return (int)(h%s->track_count); +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* s,const char* t,int cap){ - if(s==NULL||t==NULL)return; - int tr=select_track(s,t); - struct car* c=s->tracks[tr]; +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(c!=NULL){ - if(strcmp(c->value,t)==0){c->capacity+=cap;return;} - p=c;c=c->next; + while(a){ + if(strcmp(a->value,nazov)==0){ + a->capacity+=pocet; + return; + } + p=a;a=a->next; } - struct car* n=malloc(sizeof(struct car)); - if(n==NULL)return; - strncpy(n->value,t,TARGET_SIZE-1); - n->value[TARGET_SIZE-1]='\0'; - n->capacity=cap; - n->next=NULL; - if(p==NULL)s->tracks[tr]=n;else p->next=n; + 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* s,const char* t){ - if(s==NULL||t==NULL)return 0; - int tr=select_track(s,t); - struct car* c=s->tracks[tr]; - while(c!=NULL){ - if(strcmp(c->value,t)==0)return c->capacity; - c=c->next; +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* s){ - if(s==NULL)return 0; - int cnt=0; - for(int i=0;itrack_count;i++){ - struct car* c=s->tracks[i]; - while(c!=NULL){cnt++;c=c->next;} +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 cnt; + return pocet; } -int count_capacity(struct station* s){ - if(s==NULL)return 0; - int sum=0; - for(int i=0;itrack_count;i++){ - struct car* c=s->tracks[i]; - while(c!=NULL){sum+=c->capacity;c=c->next;} +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 sum; + return suma; }