c - 带有段错误的二叉搜索树

标签 c binary-search-tree

这里我想用BST结构做一些文本排序,所以我先做一些文本。但是我又遇到了段错误。好难过:

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

typedef struct BSTnode{
    struct BSTnode* leftchild;
    struct BSTnode* rightchild;
    char data[20];
}BSTnode;

void prompt();
void inputData();

BSTnode firstNode;
BSTnode* MyNode=&firstNode;     //maybe there is some better way to initialize the first node

int main()
{
    //MyNode=malloc(sizeof(BSTnode));   /* initialize the first node*/ 
    MyNode->leftchild=NULL;              
    MyNode->rightchild=NULL;
    strcpy(MyNode->data,"");    
    while(1)
        prompt();                 //always prompt the user for input
                                  //and then use the function to store that 
                                  //input in that binary tree

    return 0;
}

void prompt(){
    int i=0;     
    printf("Please select:\n");
    printf("1.input a data\n2.Exit\n");

    scanf("%d",&i);
    switch(i){
        case 1:
            inputData();
            break;
        case 2:
            exit(0);
        default:
            printf("Please input a valid number!(1 or 2)");
    }
 }

void inputData(){
    char* data="";
    printf("Input your data here(character only / less than 19 characters!): ");
    scanf("%s",data);
    BSTnode** ptr=&MyNode;
    while(1){
        if(strcmp(data,(*ptr)->data)){
            if((*ptr)->rightchild!=NULL){
                ptr=&((*ptr)->rightchild);
                continue;
            }
            else{
                (*ptr)->rightchild=malloc(sizeof(BSTnode));
                ptr=&((*ptr)->rightchild);
                (*ptr)->rightchild=NULL;
                (*ptr)->leftchild=NULL;
                strcpy((*ptr)->data,data);
                break;
            }
        }

        else{
            if((*ptr)->leftchild!=NULL){
                ptr=&((*ptr)->leftchild);
                continue;
            }
            else{
                (*ptr)->leftchild=malloc(sizeof(BSTnode));
                ptr=&((*ptr)->leftchild);
                (*ptr)->leftchild=NULL;
                (*ptr)->rightchild=NULL;
                strcpy((*ptr)->data,data);
                break;
            }
        }  
    }

    printf(" Your data have been input successfully!\n");
    return;
}

这就是所有的代码。一旦我输入一个单词并按回车键,我就会遇到段错误。 我什至不尝试打印数据。我现在要做的是输入一些数据并将其存储在二叉搜索树中。

但是,我不够熟练......其实我还没有做出成功的BST结构。因此,如果您能帮助调试,我将不胜感激。 当然,任何其他相关信息和代码也将不胜感激。

最佳答案

您在 inputData 中的数据指针指向一个常量(并且可能在堆中的某处初始化),您不应该这样做。

您需要使用 malloc 或使用固定大小的数组 f 来初始化内存。例如。

void inputData(){
    char data[100];
    memset(data,0,sizeof(data));
    printf("Input your data here(character only / less than 19 characters!): ");
    scanf("%s",data);
    BSTnode** ptr=&MyNode;

关于c - 带有段错误的二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942435/

相关文章:

c++ - C++ Switch 语句中的十六进制值

c++ - 对类型 'int' 的非常量左值引用无法绑定(bind)到类型 'int' 的临时对象

java - 如何实现自调整二叉搜索树?

c - 这里有人使用 make-cdf & stats.pl 程序吗?

c++ - 一种实用的循环展开技术

javascript - 递归调用中的返回语句

java - return 语句不将控制权返回给 java 中的调用者

c++ - 从二叉搜索树中删除一个值

c - 文件名字符串错误

c - 为什么我无法打开文件?