From abfe2cd136abd32110b56a8aaf555dba607dfe4b Mon Sep 17 00:00:00 2001 From: rz694jd Date: Sun, 1 Dec 2019 15:54:33 +0100 Subject: [PATCH] 3 --- a_station.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/a_station.c b/a_station.c index d5322dc..02792c5 100644 --- a/a_station.c +++ b/a_station.c @@ -15,9 +15,9 @@ void destroy_station(struct station* station){ struct car* start = station->tracks[i]; struct car* this = start; while(this != NULL){ - next = this->next; - free(this); - this=next; + this = start->next; + free(start); + start=this; } free(station->tracks[i]); } @@ -27,8 +27,10 @@ void destroy_station(struct station* station){ int select_track(struct station* station, const char* target){ int c; int hash = station->capacity; - char* str = target; - while (c = *str++){ + char str[36]; + char *pstr = str; + strcpy(str,target); + while ((c = *pstr++)){ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ return hash; @@ -41,10 +43,13 @@ void add_target_capacity(struct station* station,const char* target, int capacit int track = select_track(station,target); struct car* start = station->tracks[track]; if(start == NULL){ - start->value = target; + + strcpy(start->value,target); start->capacity = capacity; start->next = NULL; + + }else{ while(start->next != NULL){ 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; if(start->next == NULL){ struct car* this =(struct car*)malloc(sizeof(struct car)); - this->value = target; + strcpy(this->value,target); this->capacity = capacity; this->next = NULL; start->next = this; @@ -78,11 +83,27 @@ int get_target_capacity(struct station* station,const char* target){ } int count_targets(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 = start->next; + } + } + return count; } 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; } +