Prva verzia
This commit is contained in:
parent
007f030c33
commit
872a1e4875
35
a_station.c
35
a_station.c
@ -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){
|
||||||
|
Loading…
Reference in New Issue
Block a user