Prva verzia

This commit is contained in:
Rastislav Želonka 2019-12-01 13:52:57 +01:00
parent 007f030c33
commit 872a1e4875

View File

@ -20,13 +20,48 @@ void destroy_station(struct station* station){
} }
free(station->tracks[i]); free(station->tracks[i]);
} }
free(station);
} }
int select_track(struct station* station, const char* target){ int select_track(struct station* station, const char* target){
int c;
int hash = station->capacity;
char* str = target;
while (c = *str++){
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
return 0; return 0;
} }
void add_target_capacity(struct station* station,const char* target, int capacity){ void add_target_capacity(struct station* station,const char* target, int capacity){
int track = select_track(station,target);
struct car* start = station->tracks[track];
if(start == NULL){
start->value = target;
start->capacity = capacity;
start->next = NULL;
}else{
while(start->next != NULL){
if(strcmp(start->value,target)==0){
start->capacity = capacity;
}
start = start->next;
if(start->next == NULL){
struct car* this =(struct car*)malloc(sizeof(struct car));
this->value = target;
this->capacity = capacity;
this->next = NULL;
start->next = this;
free(this);
}
}
}
} }
int get_target_capacity(struct station* station,const char* target){ int get_target_capacity(struct station* station,const char* target){