This commit is contained in:
Maryna Kravtsova 2020-11-07 16:46:07 +01:00
parent e54978ce91
commit 12a7f384a7

View File

@ -11,6 +11,7 @@ struct station* create_station(){
void destroy_station(struct station* station){ void destroy_station(struct station* station){
for(int i = 0; i < station->track_count; i++){ for(int i = 0; i < station->track_count; i++){
//get one linked list from station
struct car* this = station->tracks[i]; struct car* this = station->tracks[i];
while(this != NULL){ while(this != NULL){
struct car* delete = this; struct car* delete = this;
@ -26,42 +27,55 @@ void destroy_station(struct station* station){
int select_track(struct station* station, const char* target){ int select_track(struct station* station, const char* target){
unsigned int hash = 0; unsigned int hash = 0;
for(int i = 0; target[i] != '\0'; i++){ for(int i = 0; target[i] != '\0'; i++){
hash = target[i] + (hash << 6) + (hash << 16) - hash; hash += target[i];
//hash = target[i] + (hash << 6) + (hash << 16) - hash;
} }
if(hash < 0 || hash > station->track_count){ //if(hash < 0 || hash > station->track_count){
hash = hash % station->track_count; hash = hash % station->track_count;
} //}
return hash; return hash;
} }
void add_target_capacity(struct station* station,const char* target, int capacity){ void add_target_capacity(struct station* station,const char* target, int capacity){
//find hash
int track = select_track(station, target); int track = select_track(station, target);
//get right linked list(with help of hash)
struct car* start = station->tracks[track]; struct car* start = station->tracks[track];
while(start != NULL){ while(start != NULL){
int x = strcmp(start->value, target); int x = strcmp(start->value, target);
//if the "wagon" track is similar to the target, then increment capacity
if(x == 0){ if(x == 0){
start->capacity = start->capacity + capacity; start->capacity = start->capacity + capacity;
return; return;
} }
start = start->next; start = start->next;
} }
struct car* newcar = calloc(1, sizeof(struct car));
strcpy(newcar->value, target); //if track is empty, create new "wagon"
newcar->capacity = capacity; if(station->track[track] == NULL){
newcar->next = start; struct car* new = calloc(1, sizeof(struct car));
station->tracks[track] = newcar; strcpy(new->value, target);
if(newcar->next){ new->capacity = capacity;
new->next = start;
station->tracks[track] = new;
/*if(new->next){
return; return;
}*/
} }
return; return;
} }
int get_target_capacity(struct station* station,const char* target){ int get_target_capacity(struct station* station,const char* target){
//find hash
int track = select_track(station, target); int track = select_track(station, target);
//get right linked list
struct car* start = station->tracks[track]; struct car* start = station->tracks[track];
while(start != NULL){ while(start != NULL){
int x = strcmp(start->value, target); int x = strcmp(start->value, target);
//if the target is similar to "wagon", than get the capacity
if(x == 0){ if(x == 0){
return start->capacity; return start->capacity;
} }
@ -70,6 +84,7 @@ int get_target_capacity(struct station* station,const char* target){
return 0; return 0;
} }
//count all tracks
int count_targets(struct station* station){ int count_targets(struct station* station){
int targets = 0; int targets = 0;
for(int i = 0; i < STATION_SIZE; i++){ for(int i = 0; i < STATION_SIZE; i++){
@ -79,10 +94,11 @@ int count_targets(struct station* station){
} }
return targets; return targets;
} }
//count capacity in all tracks
int count_capacity(struct station* station){ int count_capacity(struct station* station){
int capacity = 0; int capacity = 0;
for(int i = 0; i < STATION_SIZE; i++){ for(int i = 0; i < STATION_SIZE; i++){
//get one linked list and count there capacity
struct car* this = station->tracks[i]; struct car* this = station->tracks[i];
if(this != NULL){ if(this != NULL){
capacity = capacity + this->capacity; capacity = capacity + this->capacity;