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;
}
}
So for a homework assignment I'm supposed to parse through an input file with around 10000 cars information being make, model, color, type, price, mileage, and year and input them into a BST based on their price, when I run the code it says I have 274 bytes lost at the line where I malloc the tmp pointer. I was just wondering what the solution to this is, and also I could really any advice/help on parsing cause to me my getCars function is ugly, and it also takes around 15 seconds to run, any help is greatly appreciated!