谁能帮我找出为什么这个 C 程序可以在 VS2005 上运行但不能在 DEV-C++ 上运行

标签 c programming-languages

我有一个用于练习的 C 程序,它有一个奇怪的问题 该程序在 VS 2005 上运行良好,但在 DEV-C++ 上崩溃,问题是该练习始终针对 DEV-C++ 进行评估

该程序是关于向 BST 中插入节点,这就是问题所在... 好吧,我真的很感激一些帮助。

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

最佳答案

您的 search_pos_to_insert 在第一部分没有返回任何内容,其中 root 不为 NULL。它递归调用函数,但不收集结果。您需要返回递归调用返回的任何内容以确保正确性。

你应该改变电话

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

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

关于谁能帮我找出为什么这个 C 程序可以在 VS2005 上运行但不能在 DEV-C++ 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2776123/

相关文章:

c - 我如何用逗号分隔的文件中的 double 值填充我的数组

scala - 除了 Scala 之外,还有什么第二语言用于 LowLevel?

programming-languages - 像芥末一样的网络编程语言

math - 处理大数(50000+ 位)的最佳编码语言

javascript - JavaScript 中将函数变成对象实现了什么?

c - “struct in6_addr”没有名为 ‘s6_addr32’ 的成员,带有 -ansi

c - 如何在 FUSE 中创建符号链接(symbolic link)?

programming-languages - 哪些编程语言可以让我操纵方法中的指令序列?

c - 寻找一个代码生成器来从抽象文档描述创建 XML 读入例程

c - 我可以在使用此 mutexattr 初始化的 mutex 之前销毁 mutexattr 对象吗?