2016-11-15 63 views
1
Node *addToTree(Node *head, Node *newNode) { 
    if (head == NULL) { 
     head = newNode; 
    } else { 
     if (newNode->price < head->price) { 
      head->left = addToTree(head->left, newNode); 
     } else 
     if (newNode->price > head->price) { 
      head->right = addToTree(head->right, newNode); 
     } else 
     if (newNode->price == head->price) { 
      free(newNode); 
     } 
    } 
    return head; 
} 

Node *getCars(char *name) { 
    FILE *fp; 
    if ((fp = fopen(name, "r")) == NULL) { 
     return NULL; 
    } else { 
     Node *head = NULL; 
     Node *tmp; 
     char delim[2] = "|"; 
     char car[MAXLINELENGTH] = {0}; 
     char *token = NULL; 
     int ch; 
     while (!feof(fp)) { 
      tmp = malloc(sizeof(Node)); 
      tmp->left = tmp->right = NULL; 
      fgets(car, MAXLINELENGTH, fp); 
      token = strtok(car, delim); 
      while (token != NULL) { 
       if (strcmp(token, "model") == 0) { 
        token = strtok(NULL, delim); 
        strcpy(tmp->model, token); 
       } else 
       if (strcmp(token, "make") == 0) { 
        token = strtok(NULL, delim);  
        strcpy(tmp->make, token); 
       } else 
       if (strcmp(token, "price") == 0) { 
        token = strtok(NULL, delim);     
        tmp->price = atoi(token); 
       } else 
       if (strcmp(token, "year") == 0) { 
        token = strtok(NULL, delim);     
        tmp->year = atoi(token); 
       } else 
       if (strcmp(token, "color") == 0) { 
        token = strtok(NULL, delim);  
        strcpy(tmp->color, token); 
       } else 
       if (strcmp(token, "type") == 0) { 
        token = strtok(NULL, delim); 
        if (token == NULL) { 
         break; 
        } 
        strcpy(tmp->type, token); 
       } else 
       if (strcmp(token, "mileage") == 0) { 
        token = strtok(NULL, delim);  
        tmp->mileage = atoi(token); 
       } 
       token = strtok(NULL, delim); 
      } 
      if (check("makes.txt", tmp->make) != 1) { 
       continue; 
      } else 
      if (check("colors.txt", tmp->color) != 1) { 
       continue; 
      } else 
      if (check("types.txt", tmp->type) != 1) { 
       continue; 
      } else { 
       head = addToTree(head, tmp); 
      } 
     } 
     free(tmp); 
     fclose(fp); 
     return head; 
    } 
} 

所以對於一個家庭作業,我應該通過輸入文件與汽車10000左右的信息是,型號解析,顏色,型號,價格,行駛里程和年並根據它們的價格將它們輸入到BST中,當我運行代碼時,它說我在malloc的tmp指針處有274個字節丟失。我只是想知道這是什麼解決方案,而且我真的可以解析任何建議/幫助因爲我的getCars功能是醜陋的,它也需要大約15秒的時間運行,任何幫助,非常感謝!泄漏的內存,同時創造BST,解析需要援助

+0

變化'而{'來'而(與fgets(汽車,MAXLINELENGTH,FP)){'刪除'免費(FEOF(FP)!) (tmp);' – BLUEPIXY

+0

@BLUEPIXY你會不會也知道爲什麼在valgrind中獲得「有條件的跳轉取決於未初始化的值」錯誤?或者你會看到更多的代碼? –

+0

'continue;' - >'free(tmp);繼續;' – BLUEPIXY

回答

0

有你的代碼中的多個問題:

  • while (!feof(fp))永遠是錯的。閱讀:Why is 「while (!feof (file))」 always wrong?

  • 您應該檢查所有屬性的缺失值。

  • 當您無法匹配make,color或type時,您不會釋放節點。

  • 解析循環後不應釋放tmp:它可能已被插入到BST中,如果文件爲空,它甚至可能未初始化。

  • 令人驚訝的是,您不會將汽車插入BST而是另一輛汽車的價格相同......您應該更準確地檢查重複項。

這裏是一個改進版本:

Node *addToTree(Node *head, Node *newNode) { 
    if (head == NULL) { 
     head = newNode; 
    } else { 
     if (newNode->price < head->price) { 
      head->left = addToTree(head->left, newNode); 
     } else 
     if (newNode->price > head->price) { 
      head->right = addToTree(head->right, newNode); 
     } else 
     if (newNode->price == head->price) { 
      free(newNode); 
     } 
    } 
    return head; 
} 

Node *getCars(const char *name) { 
    FILE *fp; 
    if ((fp = fopen(name, "r")) == NULL) { 
     return NULL; 
    } else { 
     Node *head = NULL; 
     char delim[] = "|"; 
     char car[MAXLINELENGTH]; 

     while (fgets(car, sizeof car, fp)) { 
      Node *tmp = malloc(sizeof(Node)); 
      /* initialize all members */ 
      tmp->left = tmp->right = NULL; 
      tmp->model[0] = tmp->make[0] = tmp->color[0] = tmp->type[0] = '\0'; 
      tmp->price = tmp->year = tmp->mileage = 0; 
      char *token = token = strtok(car, delim); 
      while (token != NULL) { 
       if (strcmp(token, "model") == 0) { 
        token = strtok(NULL, delim); 
        if (token == NULL) break; 
        strcpy(tmp->model, token); 
       } else 
       if (strcmp(token, "make") == 0) { 
        token = strtok(NULL, delim);  
        if (token == NULL) break; 
        strcpy(tmp->make, token); 
       } else 
       if (strcmp(token, "price") == 0) { 
        token = strtok(NULL, delim);     
        if (token == NULL) break; 
        tmp->price = atoi(token); 
       } else 
       if (strcmp(token, "year") == 0) { 
        token = strtok(NULL, delim);     
        if (token == NULL) break; 
        tmp->year = atoi(token); 
       } else 
       if (strcmp(token, "color") == 0) { 
        token = strtok(NULL, delim);  
        if (token == NULL) break; 
        strcpy(tmp->color, token); 
       } else 
       if (strcmp(token, "type") == 0) { 
        token = strtok(NULL, delim); 
        if (token == NULL) break; 
        strcpy(tmp->type, token); 
       } else 
       if (strcmp(token, "mileage") == 0) { 
        token = strtok(NULL, delim);  
        if (token == NULL) break; 
        tmp->mileage = atoi(token); 
       } 
       token = strtok(NULL, delim); 
      } 
      if (check("makes.txt", tmp->make) != 1 
      || check("colors.txt", tmp->color) != 1 
      || check("types.txt", tmp->type) != 1) { 
       /* unrecognized make, type or color: discard entry */ 
       free(tmp); 
      } else { 
       head = addToTree(head, tmp); 
      } 
     } 
     fclose(fp); 
     return head; 
    } 
} 
+0

聖牛非常感謝你,這真是太神奇了。如果我刪除的部分,我釋放節點,如果價格相同,我會泄漏內存? –

+0

此外,我不能使用calloc,因爲我們還沒有在我的計算機科學課上了解到,是否可以使用原始的malloc調用tmp? –

+0

在'addToTree'中,您必須將節點插入樹中或將其釋放,所以您當前不會在那裏泄漏內存,我只是想知道是否應該丟棄具有相同價格的汽車或者是否應該擁有同樣的品牌,類型和顏色也是如此。我更新了代碼,不使用'calloc()':你必須完全初始化節點。 – chqrlie