开发者

Can anyone help me find why this C program work on VS2005 but not on DEV-C++

开发者 https://www.devze.com 2022-12-29 20:51 出处:网络
I have a C program for an exercise and it has a strange issue The program runs just fine on VS 2005 but it crashes on DEV-C++ and the problem that the problem is that the exercise is always evaluated

I have a C program for an exercise and it has a strange issue The program runs just fine on VS 2005 but it crashes on DEV-C++ and the problem that the problem is that the exercise is always evaluated against DEV-C++

The program is about inserting nodes to a BST and this is where the problem lies... Well i would really appreciate some help.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct tree_node   
{
int value;
int weight;
struct tree_node *left; 
struct tree_node *right; 
} TREE_NODE;
   TREE_NODE *create_tree(int list[], int size);
TREE_NODE *search_pos_to_insert(TREE_NODE *root, int value, int *left_or_right);    
//     this is the problematic function */
void inorder(TREE_NODE *root); /* Inorder Traversing */
TREE_NODE *temp;
int main()
{
TREE_NODE *root; /* Pointer to the root of the BST */
int values[] = {10, 5, 3, 4, 1, 9, 6, 7, 8, 2}; /* Values for BST */
int size = 10, tree_weight;

root = create_tree(values, 10);
printf("\n");

inorder(root); /* Inorder BST*/
system("PAUSE");
}


TREE_NODE *search_pos_to_insert(TREE_NODE *root, int value, int *left_or_right)
{
if(root !=NULL)
{
temp = root;
if(value >root->value)
{
    *left_or_right=1;
    *search_pos_to_insert(root->right, value, left_or_right);   
}
else
{
    *left_or_right=0;
    *search_pos_to_insert(root->left, value, left_or_right);
}
}
else

return temp;/* THIS IS THE PROBLEM (1) */


}
TREE_NODE *create_tree(int list[], int size)
{
TREE_NODE *new_node_pntr, *insert_point, *root = NULL;
int i, left_or_right;

/* First Value of the Array is the root of the BST */
new_node_pntr = (TREE_NODE *) malloc(sizeof(TREE_NODE));
new_node_pntr->value = list[0]; 
new_node_pntr->weight = 0;
new_node_pntr->left = NULL;
new_node_pntr->right = NULL;
root = new_node_pntr; 
/* Now the rest of the arrat. */
for (i = 1; i < size; i++)
{
/* THIS IS THE PROBLEM (2) */

 开发者_开发问答   insert_point = search_pos_to_insert(root, list[i], &left_or_right); 

    /* insert_point just won't get the return from temp */
    new_node_pntr = (TREE_NODE *) malloc(sizeof(TREE_NODE));
    new_node_pntr->value = list[i];
    new_node_pntr->weight = 0;
    new_node_pntr->left = NULL;
    new_node_pntr->right = NULL;
    if (left_or_right == 0)
        insert_point->left = new_node_pntr;
    else
        insert_point->right = new_node_pntr;
}
return(root);
}


void inorder(TREE_NODE *root)
{
if (root == NULL)
    return;
inorder(root->left);
printf("Value: %d, Weight: %d.\n", root->value, root->weight);
inorder(root->right);
}


Your search_pos_to_insert isn't returning anything in the first section, where root is not NULL. It is recursively calling the function, but not gathering the result. You need to return whatever your recursive calls return to ensure correctness.

You should change the calls

*search_pos_to_insert(root->right, value, left_or_right);
...
*search_pos_to_insert(root->left, value, left_or_right);

to

return search_pos_to_insert(root->right, value, left_or_right);
...
return search_pos_to_insert(root->left, value, left_or_right); 


While I haven't delved deep into many issues, are you sure your use of "temp" as a global is correct here? Should it not be local to the search function, so the function is reentrant?


In your function TREE_NODE *search_pos_to_insert() you have code pathes, that don't return a value. Your compiler should issue a warning about it.

The line:

return temp;/* THIS IS THE PROBLEM (1) */

is reached only if the if(root != NULL) evaluates to true.

Replace the recursive calls with:

return search_pos_to_insert(root->right, value, left_or_right);
return search_pos_to_insert(root->left, value, left_or_right); 

to make it work.


Frank your the man .. In some moment i thought that maybe i could return the function itself but i was almost sure it wouldn't work and also i took a different road (to left_or_right) and i was completely lost Well u saved from a lot of anger and u really saved my day(probably a lot more)

0

精彩评论

暂无评论...
验证码 换一张
取 消