[英]Binary tree insert ( only get the last item…)
I'm trying to load a file which has couple of infor, and I want to add them into a binary tree. 我正在尝试加载一个包含几个infor的文件,我想将它们添加到一个二叉树中。 For sure there is no problem with addToTree and printPreOrder functions, but i don't know why I can only print ( or maybe add ) the last item in the file.
当然,addToTree和printPreOrder函数没有问题,但是我不知道为什么我只能打印(或添加)文件中的最后一项。 Whats the problem?
有什么问题?
newTree = createBinTree(&cmpInt, &destroyNode);
newRes = createRes();
while (fgets(buffer, 100, fp) != NULL)
{
strcpy(newRes->name,strtok(buffer, ","));
strcpy(newRes->food,strtok(NULL, ","));
newRes->star = atoi(strtok(NULL, ","));
addToTree(newTree, newRes);
}
printPreOrder(newTree, &printNode);
You are always adding to the same pointer to newRes
. 您总是将相同的指针添加到
newRes
。 While reading you overwrite your previous newRes
entries. 阅读时,您将覆盖以前的
newRes
条目。 You need to call createRes
for each object you are reading. 您需要为正在读取的每个对象调用
createRes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.