在C-PSET5CS50中加载trie数据结构

在C-PSET5CS50中加载trie数据结构,c,load,trie,cs50,C,Load,Trie,Cs50,我无法将数据加载到我的trie结构中。我老是犯错误。这和我的malloc有关吗?有人看到什么问题吗 谢谢 /** * dictionary.c * * Computer Science 50 * Problem Set 5 * * Implements a dictionary's functionality. */ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #inc

我无法将数据加载到我的trie结构中。我老是犯错误。这和我的malloc有关吗?有人看到什么问题吗

谢谢

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "dictionary.h"


#define ASCII_OFFSET 97;

/** Node of the data structure used to store the dictionary key words
 * Data Structures Type is Tries
 * Includes a bool to indicate if the current node is the end of a word
 * A pointer to the nodes child node
 */
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;
node* rootNode;
node* nextNode;

//Variable to track the size of the dictinoary
int wordCount = 0;

/**
 * Returns true if word is in dictionary else false.
 */

bool check(const char* word)
{
    //Get words length
    int wordLength = strlen(word);

    for(int i = 0; i < wordLength; i++)
    {
        //taking the character we need to check
        int charToCheck = tolower(word[i]) - ASCII_OFFSET;

        //Checking to see if the char exists in the data strucutre, Trie;
        if(nextNode->children[charToCheck] == NULL)
        {
            return false;
        }

        //Advance the next node down the trie
        nextNode = nextNode->children[charToCheck];
    }

    nextNode = rootNode;
    //Return what is_word return
    return nextNode->is_word;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */

bool load(const char* dictionary)
{
    //Open dict.. file to read 
    FILE* file = fopen(dictionary,"r");

    //Check if the dict.. exsists!
    if(file == NULL)
    {
        return false;
    }

    //Creating the first node in our data strucutre
    rootNode = malloc(sizeof(node));
    nextNode = rootNode;

    //Get character to store
    int character = fgetc(file) - ASCII_OFFSET;

    //Go through the dict... file 
    while(character != EOF)
    {
        //Go through each word in the file
        while(character != '\n')
        {
            //Add into our data structure
            if(nextNode->children[character] == NULL)
            {
                //Create memory inorder to insert the next node
                nextNode->children[character] = malloc(sizeof(node));
                nextNode = nextNode->children[character];
            }else {
                nextNode = nextNode->children[character];
            }

            //advance character to next
            character = fgetc(file) - ASCII_OFFSET;
        }

        //advance character to next word
        character = fgetc(file) - ASCII_OFFSET;

        //Set the last node loaded to is_word to track the end of each word
        nextNode->is_word = true;
        wordCount++;
        nextNode = rootNode;
    }


    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */

unsigned int size(void)
{
    return wordCount;
}

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */

bool unload(void)
{
    for(int i = 0; i < 26; i++)
    {
        if(nextNode->children[i] != NULL)
        {
            nextNode = nextNode->children[i];
            unload();
        }
    }

    free(nextNode);
    return true;
}
/**
*字典
*
*计算机科学50
*习题集5
*
*实现字典的功能。
*/
#包括
#包括
#包括
#包括
#包括
#包括“dictionary.h”
#定义ASCII_偏移量97;
/**用于存储字典关键字的数据结构的节点
*数据结构类型为
*包括一个bool,用于指示当前节点是否为单词的结尾
*指向节点子节点的指针
*/
类型定义结构节点
{
布尔是一个词;
结构节点*子节点[27];
}
节点;
node*rootNode;
节点*nextNode;
//变量来跟踪字典的大小
int字数=0;
/**
*如果单词在字典中,则返回true,否则返回false。
*/
布尔检查(常量字符*单词)
{
//获取字长
int-wordLength=strlen(字);
for(int i=0;ichildren[charToCheck]==NULL)
{
返回false;
}
//沿trie前进下一个节点
nextNode=nextNode->children[charToCheck];
}
nextNode=rootNode;
//返回什么是返回
返回nextNode->is\u单词;
}
/**
*将字典加载到内存中。如果成功,则返回true;否则返回false。
*/
布尔加载(常量字符*字典)
{
//打开要读取的dict..文件
FILE*FILE=fopen(字典,“r”);
//检查该命令是否存在!
if(file==NULL)
{
返回false;
}
//在数据结构中创建第一个节点
rootNode=malloc(sizeof(node));
nextNode=rootNode;
//获取要存储的字符
int character=fgetc(文件)-ASCII_偏移量;
//检查一下口述文件
while(字符!=EOF)
{
//检查文件中的每个单词
while(字符!='\n')
{
//添加到我们的数据结构中
if(nextNode->children[字符]==NULL)
{
//创建内存以插入下一个节点
nextNode->children[character]=malloc(sizeof(node));
nextNode=nextNode->children[字符];
}否则{
nextNode=nextNode->children[字符];
}
//将字符前进到下一个
字符=fgetc(文件)-ASCII_偏移量;
}
//将字符前进到下一个单词
字符=fgetc(文件)-ASCII_偏移量;
//将加载到is_单词的最后一个节点设置为跟踪每个单词的结尾
nextNode->is_word=true;
字数++;
nextNode=rootNode;
}
fclose(文件);
返回true;
}
/**
*如果已加载,则返回字典中的字数;如果尚未加载,则返回0。
*/
无符号整数大小(void)
{
返回字数;
}
/**
*从内存中卸载字典。如果成功,则返回true;否则返回false。
*/
bool卸载(无效)
{
对于(int i=0;i<26;i++)
{
if(nextNode->children[i]!=NULL)
{
nextNode=nextNode->children[i];
卸载();
}
}
免费(下一个节点);
返回true;
}
  • 在将字符用作数组中的索引之前,不必检查字符是否在范围内。任何超出a-z范围的字符都将导致缓冲区溢出

  • 从中减去97后,将该字符与已知字符常量进行比较

  • 您需要通过将
    NULL
    分配给所有数组元素并将
    is\u word
    设置为false来初始化从malloc返回的内存


  • 只是简单地看了一下代码,所以我可能错过了其他错误。

    恭喜你,你已经将作业粘贴到了互联网上。你的问题是什么?抱歉,我加载我的trie时遇到问题,不断出现分段错误。“trie”是什么?(Ssh,不要帮助OP!)在
    #define ASCII#u OFFSET 97中的分号。但是,如果代码是用它编译的,那么您是否真的使用了
    ASCII\u OFFSET
    ?(答:是的,至少使用了两次,但是在表达式的末尾,因此额外的分号只是一个空语句。)使用
    #define ASCII_OFFSET'a'
    会更清楚吗?您没有正确初始化分配的trie结构元素。您需要确保所有这些指针都为空。您还应该使用calloc而不是malloc,因为您检查了NULL,但malloc没有给您定义的内存值。@maxbit89您提出了一个有效的点。我不认为标准要求
    NULL
    必须是
    0
    (即使“所有”实现似乎都将其设置为
    0
    ),因此我选择建议显式地将指针设置为
    NULL
    。是的,标准中没有定义它,但它使用得很好。另见: