简体   繁体   English

具有segfault的二叉搜索树

[英]A binary search tree with segfault

Here I want to make some text sorting with a BST structure, so I first do some text first. 在这里,我想使用BST结构进行一些文本排序,所以我首先要做一些文本。 But I get segfault again. 但是我再次遇到段错误。 So sad about it: 很伤心:

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

That is all the code. 这就是全部代码。 And I get segfault once I input a word and press return. 输入一个单词并按回车键,就会出现段错误。 I don't even try to print the data out. 我什至不尝试打印数据。 What I want to do now is input some data and store it in a binary search tree. 我现在想做的是输入一些数据并将其存储在二进制搜索树中。

But, I not skillful enough... Actually I haven't make a successful BST structure. 但是,我不够熟练……实际上我还没有建立一个成功的BST结构。 So I would really appreciate it if you can help debug. 因此,如果您能帮助调试,我将不胜感激。 Of cource, any other related information and code would also be appreciated. 当然,任何其他相关信息和代码也将被理解。

Your data pointer in inputData is pointing to a constant (and that maybe initialized somewhere in the heap), you shouldn't do that. 您在inputData中的数据指针指向一个常量(并且可能在堆中的某个地方初始化了),您不应该这样做。

You need to initialize memory with malloc or, use a fixed-size array, f. 您需要使用malloc初始化内存,或者使用固定大小的数组f。 ex. 例如

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM