printf与scanf请求输入。 BST循环错误

我一直试图看看为什么printf在打印文件输入后没有打破循环。 .c文件是BST,我现在正在测试是否已经构建了一个树,但似乎无法退出printf循环。 我需要printf循环来获得代码的正确输出。 有关此错误发生原因的任何建议。 显示完整的代码和输出。

#include "bst.h" #include  #include  ///Author: Joe W //arbitrary list of temp nodes TreeNode *new_node, *root, *tmp, *parent; int elemArray[100], i1, i2, i0; /* Insert a new node into the tree by referencing the root and using recursion */ TreeNode* getN(int dataElem) { TreeNode* temp; temp = malloc(sizeof(TreeNode*)); temp-> left = NULL; temp-> right = NULL; temp->data = dataElem; return temp; } TreeNode* addNodeToTree(TreeNode *root, int data) { TreeNode* newNode = malloc(sizeof(TreeNode*)); if (root == NULL) { // thingy too. Creating a function too, // for you to look at. root = newNode; return root; } else { TreeNode* parent = malloc(sizeof(TreeNode*)); TreeNode* current = malloc(sizeof(TreeNode*)); parent = current = root; // This loop will actually help us, find the `parent`, // of the `newNode`(which is to be inserted) while (current != NULL) { parent = current; if (current->data > data) current = current->left; else if (current->data right; } // Now once, we have found, the node, under which the // newNode will be placed, now we have to check, whether // newNode will become a `left child/right child` of the // parent. newNode = getN(data); if (parent->data > data) parent->left = newNode; else if (parent->data right = newNode; return root; } } void build_Tree(TreeNode** root, const int elements[], const int count) { //TreeNode* node = malloc(sizeof(TreeNode*)); //node->left = node ->right = NULL; for ( i0 = 0; i0 < count; ++i0 ){ *root = addNodeToTree(*root, elements[count]); } } 

编译好了。 文件获取用户输入并开始显示代码后,错误开始。 用printf语句。

 int main( int argc, char *argv[] ) { //Throw error is *Argv[] is not an integer //assuming it was an integer int cnt = atoi( argv[1] ); printf( "number is %d\n", cnt ); // printf("Enter %i integer values to place in tree:\n", cnt); for ( i1 = 0; i1 < cnt; ++i1) { scanf( "%d", &elemArray[i1] ); } //first ouput "INput Values" printf( " Input Values:\n " ); for ( i2 = 0; i2 data= 5; traverse( *root, PREORDER); //pass the pointer of root to traverse the tree //traverse a single node printf( "Inorder:\n "); printf( "Postorder:\n "); //Build tree with each element return 0; } void traverse( const TreeNode* root, const TraversalType type ) { if ( type == PREORDER) { if (root != NULL) { printf("%d", root->data); traverse( root->left, PREORDER); traverse( root-> right, PREORDER); } } } /** void insertNode(TreeNode** root, TreeNode* new_node) { if (new_node-> data  data) { if (*root-> left == NULL) *root-> left == new_node; else insert(*root->left, new_node); } if (new_node->data > *root->data) { if(*root-> right ==NULL) *root->right = new_node; else insert(*root->right, new_node); } } **/ //question1: what is the 

这是我开始运行代码的地方

 jw8453@idaho:~/Courses/CS243/Homework/5$ make gcc -ggdb -c bst.c gcc -ggdb -o bst bst.o -lm jw8453@idaho:~/Courses/CS243/Homework/5$ ./bst 5 number is 5 Enter 5 integer values to place in tree: 1 2 3 4 5 Input Values: 1 building tree0 2 building tree0 3 building tree0 4 building tree0 5 building tree0 this isnt supposed to be in the printf loop 

注意:问题是BST构建树双指针的延续

这是错的:

 TreeNode* newNode = malloc(sizeof(TreeNode*)); 

代替

 TreeNode* newNode = malloc(sizeof(TreeNode)); 

要么

 TreeNode* newNode = malloc(sizeof(*newNode)); 

这里:

  TreeNode* parent = malloc(sizeof(TreeNode*)); TreeNode* current = malloc(sizeof(TreeNode*)); parent = current = root; 

你有内存泄漏,你保留parent和空的空间,然后你将这些变量分配给另一个地址,相同的:

 TreeNode* newNode = malloc(sizeof(TreeNode*)); ... newNode = getN(data); 

我建议

 void setN(TreeNode *node, int dataElem) { node->left = NULL; node->right = NULL; node->data = dataElem; } ... setN(newNode, data); 

代替

 TreeNode* getN(int dataElem) { TreeNode* temp; temp = malloc(sizeof(TreeNode*)); temp-> left = NULL; temp-> right = NULL; temp->data = dataElem; return temp; } ... newNode = getN(data);