0

我正在尝试在 C 中实现二叉搜索树。我正在使用带有 MinGW 编译器的代码块 ide

当我尝试运行以下代码时,此运行时错误 进程返回进程返回 0xC00000fd

但是当我在http://ideone.com/编译时, 它工作正常,没有任何错误

已解决:感谢@user1161318

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

struct node
{
    struct node *left;
    struct node *right;
    struct node *parent;
    int value;
}*r;

void inorder(struct node *root)
{
    int sam;
    if(root)
    {
        inorder(root->left);
        sam = root->value;
        printf(" %d ->",sam);
        inorder(root->right);
    }
}

void insert(struct node *root,int x)
{
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->value = x;
    struct node *y=root;
    while(root)
    {
        y = root;
        if(root->value > x)
        {
            root = root->left;
        }
        else
        {
            root = root->right;
        }
    }
    temp->parent = y;
    if(!y)
    {
        r=temp;
    }
    else if(x > y->value)
    {
        y->right = temp;
    }
    else
    {
        y->left = temp;
    }

}

int main()
{
    int i;
    for(i=0; i<10; i++)
    {
        insert(r,i);
    }
    inorder(r);
    return 0;
}
4

1 回答 1

1

全局变量struct node *r未在 中初始化main()

于 2012-12-07T22:02:34.660 回答