This commit is contained in:
Rastislav Želonka 2019-12-01 15:54:33 +01:00
parent 15fda00eee
commit abfe2cd136

View File

@ -15,9 +15,9 @@ void destroy_station(struct station* station){
struct car* start = station->tracks[i]; struct car* start = station->tracks[i];
struct car* this = start; struct car* this = start;
while(this != NULL){ while(this != NULL){
next = this->next; this = start->next;
free(this); free(start);
this=next; start=this;
} }
free(station->tracks[i]); free(station->tracks[i]);
} }
@ -27,8 +27,10 @@ void destroy_station(struct station* station){
int select_track(struct station* station, const char* target){ int select_track(struct station* station, const char* target){
int c; int c;
int hash = station->capacity; int hash = station->capacity;
char* str = target; char str[36];
while (c = *str++){ char *pstr = str;
strcpy(str,target);
while ((c = *pstr++)){
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash; return hash;
@ -41,10 +43,13 @@ void add_target_capacity(struct station* station,const char* target, int capacit
int track = select_track(station,target); int track = select_track(station,target);
struct car* start = station->tracks[track]; struct car* start = station->tracks[track];
if(start == NULL){ if(start == NULL){
start->value = target;
strcpy(start->value,target);
start->capacity = capacity; start->capacity = capacity;
start->next = NULL; start->next = NULL;
}else{ }else{
while(start->next != NULL){ while(start->next != NULL){
if(strcmp(start->value,target)==0){ if(strcmp(start->value,target)==0){
@ -53,7 +58,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
start = start->next; start = start->next;
if(start->next == NULL){ if(start->next == NULL){
struct car* this =(struct car*)malloc(sizeof(struct car)); struct car* this =(struct car*)malloc(sizeof(struct car));
this->value = target; strcpy(this->value,target);
this->capacity = capacity; this->capacity = capacity;
this->next = NULL; this->next = NULL;
start->next = this; start->next = this;
@ -78,11 +83,27 @@ int get_target_capacity(struct station* station,const char* target){
} }
int count_targets(struct station* station){ int count_targets(struct station* station){
int count=0;
return 0; for(int i = 0;i < station->capacity;i++){
struct car* start = station->tracks[i];
while(start->next != NULL){
count++;
start = start->next;
}
}
return count;
} }
int count_capacity(struct station* station){ int count_capacity(struct station* station){
return 0; int count=0;
for(int i = 0;i< station->capacity;i++){
struct car* start = station->tracks[i];
while(start->next != NULL){
count =+ start->capacity;
start = start->next;
}
}
return count;
} }