运行时“.exe文件已停止工作”

时间:2011-03-01 16:53:58

标签: c

下面的程序编译得很好,但是当我运行它时会出现错误“.exe文件啊停止工作”。请帮忙。有什么建议吗?

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

#define MAXSIZE 500 

struct key 
{ 
    int item; 
    struct key *left; 
    struct key *right; 
}; 

typedef struct key star;  

void make_tree(star *link, int j, char a[]); 
int find_num_of_leaves(star *node); 

star *tree; 
char input[MAXSIZE]; 

int main() 
{ 
    int i=0; 
    int num; 

    printf("Enter the number:\n"); 
    scanf("%[^\n]", input); 

    tree = (star *) malloc(sizeof(star)); 
    tree->item = -1; 

    make_tree(tree, i, input); 
    printf("@@@@@@@"); 
    num = find_num_of_leaves(tree); 
    printf("#######"); 
    printf("\n\n%d", num); 

    return(0); 

} 

void make_tree(star *link, int j, char a[]) 
{ 
    if(a[j] == '\0') 
    { 
        link->left = NULL; 
        link->right = NULL; 
        return; 
    } 

    if(a[j+1] == '\0') 
    { 
        link->right = NULL; 
        return; 
    } 

    if(int(a[j]) > 0) 
    { 
        link->left = (star *) malloc(sizeof(star)); 
        (link->left)->item = a[j]; 
        return make_tree(link->left, j+1, a); 
    } 

    if(((10*int(a[j])) + int(a[j+1])) <= 26) 
    { 
        link->right = (star *) malloc(sizeof(star)); 
        (link->right)->item = (10*int(a[j])) + int(a[j+1]); 
        return make_tree(link->right, j+1, a); 
    } 

} 

int find_num_of_leaves(star *node) 
{ 
    if(node == NULL) 
        return 0;     
    if(node->left == NULL && node->right == NULL) 
        return 1; 
    else 
        return find_num_of_leaves(node->left) + find_num_of_leaves(node->right); 
    /*if(node->left == NULL) 
        find_num_of_leaves(node->right); 

    if(node->right == NULL) 
        find_num_of_leaves(node->left); 

    if(node->right != NULL && node->left != NULL) 
    { 
        find_num_of_leaves(node->left); 
        find_num_of_leaves(node->right); 
    }*/ 

} 

1 个答案:

答案 0 :(得分:2)

当创建make树时,如果代码通过此位,则链接&gt; left不会被初始化: (例如,如果您在控制台中输入一个数字)

if(a[j+1] == '\0')
{
      link->right = NULL;
      return;
}  

因此,当您的代码调用find_num_of_leaves时,它会调用find_num_of_leaves,当它尝试取消引用节点的左侧部分时会崩溃。