【问题标题】:BST dictionary error?BST字典错误?
【发布时间】:2016-12-05 02:45:26
【问题描述】:

我正在构建一个 BST 字典树。我在strcmp 行遇到错误,但我不知道为什么。错误是EXC_BAD_ACCESS(Code = 1)。这棵树只是一棵右树,因为文件的第一行是a,所以它是没有左孩子的根。我在错误行旁边评论了。

文本的前几行;

a   un, uno, una[Article]
aardvark    cerdo hormiguero
aardvark    oso hormiguero[Noun]
aardvarks   cerdos hormigueros
aardvarks   osos hormigueros

我的代码;

#include <stdio.h>


typedef struct node{
    char lineOfChar[200];
    struct node *left;
    struct node *right;
}BST;

typedef struct nodeSec{
    BST *next;
}mainLink;

int main(int argc, const char * argv[]) {

    struct node *head = malloc(sizeof(BST)); //Define the Head of the linked list of BST
    struct nodeSec *headOfHead = malloc(sizeof(mainLink));
    headOfHead->next = head;
    struct node *current = &head; //Progress down the tree
    FILE *file;
    file = fopen("/Users/bassammetwally/Desktop/Homework5/text","r");
    int times = 0;
    char arrayOfLine[200];

    while(fgets(arrayOfLine, 200, file)){ //While loop to read lines into the node's array

        if (times == 0) // head of linked list to run once
        {
            for (int counterFirstTime = 0; counterFirstTime < 140; counterFirstTime++)
            {
                head->lineOfChar[counterFirstTime] = arrayOfLine[counterFirstTime]; // copy the array into the node array
            }
            times++; // increase times so the previous block would not repeat
        }
        else {

            struct node *temp = malloc(sizeof(BST)); //start a temp node to store the current information for further sorting
            for (int counterFirstTime = 0; counterFirstTime < 40; counterFirstTime++)
            {
                temp->lineOfChar[counterFirstTime] = arrayOfLine[counterFirstTime];//copying line into the node array
            }
            int q = 2;
            while (q == 2)
            {
                int characterCompare;
                if (head != NULL && temp != NULL)
                {
                characterCompare = strcmp(temp->lineOfChar, head->lineOfChar);//compares the two characters of the array WHERE I GET THE ERROR
                }

                if (characterCompare > 0) // if temp is bigger..
                {
                    current = head;
                    head = head->right;// go to the right of the tree
                    if (head == NULL)// if its NULL and empty then just store it there
                    {
                        head = malloc(sizeof(BST));
                        head = temp;
                        current->right= head;
                        break;
                    }
                }
                else if (characterCompare < 0){ //since there will not be any duplicates
                    current = head;
                    head = head->left;
                    if (head == NULL)// if its NULL and empty then just store it there
                    {
                        head = malloc(sizeof(BST));
                        head = temp;
                        current->left = head;
                        break;
                    }
                }
                else if( characterCompare == 0)
                {
                    continue;
                }

            }
        }
        head = headOfHead->next;
    }
    return 0;
}

【问题讨论】:

  • 您目前是否只是想将定义插入到您的树中?

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


【解决方案1】:

指针有一些问题。请注意,“head”是指向“node”结构的指针。在行中;

  struct node *current = &head;

您没有将当前分配给指向头,而是将当前分配给指向指针头(您正在获取指针的地址)。

您还设置了 headOfNext 指向 head 然后稍后使用;

  head = headOfHead->next;

你只是把头放回自己。

最后,nodesec 不需要 struct。它只包含一个元素,因此您可以只使用该元素。 (除非您有扩展结构的未来计划。)

【讨论】:

  • 但是这些问题应该不会导致错误吧?即使我修复了同样的错误。
  • 如果你修复它们,你的代码将会完全不同。让我们看看。
猜你喜欢
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
相关资源
最近更新 更多