diff --git a/a_station.c b/a_station.c index 931980f..7647b32 100644 --- a/a_station.c +++ b/a_station.c @@ -20,13 +20,48 @@ void destroy_station(struct station* station){ } free(station->tracks[i]); } + free(station); } 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; } 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){