与scanf函数要求输入的printf。与BST环路误差 [英] Printf with scanf asking for input. Loop error with BST

查看:175
本文介绍了与scanf函数要求输入的printf。与BST环路误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解为什么printf的不打印文件的输入之后,打破循环。 .c文件是BST和我现在在测试树是否已建成,但似乎无法退出的printf循环的中间。我需要正确的输出为code中的printf的循环。为什么这个错误发生的任何建议。全code和输出显示。

I have been trying to see why printf does not break the loop after printing the input of the file. The .c file is BST and I am now in the middle of testing whether a tree has been built but can't seem to exit the printf loop. I need the printf loop for the correct output for the code. Any suggestions of why this error is happening. Full code and output is shown.

#include "bst.h"
#include <stdio.h>
#include <stdlib.h>


///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 < data)
                current = current->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 < data)
            parent->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]);
    }

}

这编译罚款。该文件需要用户输入,并开始显示code后的错误开始。用printf的语句。

This compiles fine. The error begins after the file takes user input and begins to display the code. with printf statements.

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 < cnt; ++i2) {
               printf( "%d\n", elemArray[i2] );
               printf("building tree0\n");
        }
    printf("building tree");
    TreeNode** root = malloc(sizeof(TreeNode*));
    printf("building tree");
    build_Tree(root, elemArray, cnt );
    printf("Tree Built");
    printf( "Preorder:\n ");

    //traverse
    //TreeNode* tempN = malloc(sizeof(TreeNode*));
    //tempN->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 < *root-> 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 

这是我开始运行code

This is where I begin to run the code

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

请注意:问题是<一个延续href=\"http://stackoverflow.com/questions/26395841/bst-build-tree-double-pointers/26396458?noredirect=1#comment41445596_26396458\">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;

您有内存泄漏,您预留空间电流,然后分配这些变量到另一个地址,同样为:

you have a memory leak, you reserve space for parent and current and then you assign those variables to another address, same for:

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);

这篇关于与scanf函数要求输入的printf。与BST环路误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆