从链表中构建BST时崩溃

时间:2018-01-20 15:52:53

标签: c linked-list binary-search-tree

首先,我从包含文本文件的目录中读取所有单词及其位置,并将它们放在链接列表中。为此,我首先逐行读取每个文本文件,然后读取每个文本文件中的单词。将它们存储在链表中(直到此处程序完美运行)  然后我想构建一个包含链表中所有单词的BST。

以下是代码:

        typedef struct node {
             char word[100];
             char FileName[100];
             struct node* next;
        } node;  

        typedef struct BSTnode {
            char word[100];
            struct BSTnode* right;
            struct BSTnode* left;
            node* head2;
        }BSTnode;

        BSTnode* root=NULL;
        node* head=NULL;

void readAllFiles(char *path)
{
    struct stat status;
    struct dirent *object;
    DIR *dir;

    if ( (dir = opendir(path)) == NULL)
    {
        printf("Sorry, unable to open %s directory", path);
        return;
    }

    chdir(path);

    while ( (object = readdir(dir)) != NULL)
    {
        stat(object->d_name, &status);
        if (S_ISDIR(status.st_mode))
        {
            if (strcmp("..", object->d_name) == 0 || strcmp(".", object->d_name) == 0) continue;
            readAllFiles(object->d_name);
            chdir("..");
        }

        else
        {
            linebyline(object->d_name);
        }
    }
}

void linebyline (char* file1){
    int i=0,j;
    char sample [1000][1000];
    FILE* ptr;
    ptr=fopen(file1,"r");
    while (!feof (ptr)) {
        fgets(sample[i],1000,ptr);
        i++;
    }
    for (j=0;j<i;j++) {
        word(sample[j],file1);
    }
}

void word (char s1[],char* file1) {
    char a[100];
    int i,j=0,k=0,length;
    length=strlen (s1);
    for (i=0;i<length+1;i++) {
        if (!((s1[i]>='a' && s1[i]<='z') || (s1[i]>='A' && s1[i]<='Z'))) {
        a[j]='\0';
        if (a[0]>='A') {
    insert(a,file1);
  //  printf("%s\n",a);
    }
        j=0;
    }
    else {
        a[j]=s1[i];
        j++; }


    }
}

void insert (char string [],char* file1) {
    node* a=(node*)malloc(sizeof(node));
    node* temp=(node*)malloc(sizeof(node));
    strcpy(a->word,string);
    strcpy(a->FileName,file1);
    if (head==NULL) {
        head=(node*)malloc(sizeof(node));
        strcpy(head->word,a->word);
        strcpy(head->FileName,a->FileName);
        head->next=NULL;
    }
    temp=head;
    while (temp->next!=NULL) {
        temp=temp->next;
    }
    temp->next=a;
    a->next=NULL;
}

        BSTnode* BSTinsert(BSTnode* root, node* data)
        { 

          if(root == NULL) {
            BSTnode* new_node =(BSTnode*)malloc(sizeof(BSTnode));
            strcpy(new_node->word,data->word);
            new_node->left = NULL;
            new_node->right = NULL;
              return new_node; }

            else if (strcmp (data->word,root->word)>0) {
            root->right = BSTinsert(root->right, data); }

            else {
            root->left = BSTinsert(root->left, data);
          }

          return root;

     }

    void TransferToBST (BSTnode* root,node* head) {

        while (head->next!=NULL) {
                root=BSTinsert(root,head);
                head=head->next;
        }
    }

    int main () {
        readAllFiles("m");
        TransferToBST(root,head);
        while (root->right!=NULL){
           printf("%s\n",root->word);
           root=root->right;}

当我在CodeBlocks上运行此代码时,它崩溃了,我不知道为什么。感谢您的帮助和评论。

1 个答案:

答案 0 :(得分:0)

您无法取消引用NULL指针:

BSTnode* root=NULL;
node* head=NULL;

然后:

int main () {
    TransferToBST(root,head);
}

void TransferToBST (BSTnode* root,node* head) {

看看:

   while (head->next!=NULL) {

Head NULL访问head->next会导致崩溃。