为什么即使分配给同一类型的另一个结构,该结构仍保持 NULL?
Why does the struct remain NULL even when assigned to another struct of the same type?
我正在尝试生成字符串的二叉树。列表中包含的结构是使用以下代码创建的:
typedef struct WordNode {
char *word;
unsigned count;
struct WordNode *left;
struct WordNode *right;
} wordnode;
我在主函数中首先将根节点初始化为 NULL,如下所示:
// The initial struct begins empty.
wordnode *head = NULL;
这是因为初始结构将是给定输入文件中包含的第一个单词。初始化为 NULL 后,我开始处理文件内容:
// While the end of file marker has not been reached...
while (EOF != fscanf(inHandle, BUFFMT, wordBuffer)) {
// Clean the word currently in the wordBuffer.
cleanWord(wordBuffer);
// If the buffer contains an actual word...
if (strlen(wordBuffer) > 0) {
// Add one to the total word count.
totalWords++;
placeWordInTree(wordBuffer, head);
}
}
placeWordInTree函数处理输入文件的每个单词(占第一个空根节点):
void placeWordInTree(char *word, wordnode *currNode) {
// If the current node is empty, initialize it to a newly generated node.
if (currNode == NULL) {
currNode = generateNode(word);
// If the base case has not been satisfied...
} else {
// Store the result of comparing the given word and the current structs word.
int strComp = strcmp(word, currNode->word);
// If the words are equal, add one to the current structs counter.
if (strComp == 0) {
currNode->count++;
// If the given word is less than the struct word, recursively call this function
// with the given node and the current structs left node.
} else if (strComp < 0) {
placeWordInTree(word, currNode->left);
// If the given word is greater than the struct word, recursively call this
// function with the given word and the current structs right node.
} else if (strComp > 0) {
placeWordInTree(word, currNode->right);
}
}
}
这个函数检查当前节点是否为NULL。如果是,它将使用给定单词的 generateNode 函数生成的新结构分配给当前结构。如果当前结构不为空,它将给定字与当前结构字进行比较,然后将计数器加一或尝试递归地将节点放置在当前结构左侧的节点中(如果它小于当前结构字)或正确的结构(如果更多)。
generateNode 函数生成一个这样的节点:
wordnode *generateNode(char *word) {
// Allocates enough memory to hold a new word node.
wordnode *newNode = malloc(sizeof(wordnode));
// Allocates enough memory to store the new nodes given word.
newNode->word = malloc(strlen(word)+1);
// Copies the given word into the new nodes word field.
strncpy(newNode->word, word, strlen(word));
// Sets the new nodes count to 1, and its branches to NULL.
newNode->count = 1;
newNode->left = NULL;
newNode->right = NULL;
// Return the new node.
return newNode;
}
我遇到的问题是程序似乎认为在 placeWordInTree 函数中检查 head 结构时它始终为 NULL。当放置用于调试的 printf 语句时,它告诉我当 placeWordInTree 函数的每个可能结果时,它总是执行代码,就好像 head struct 总是 NULL 一样。更令人困惑的是,当打印树的内容时,head 结构包含的单词始终是输入文件的第一个单词。经过漫长的调试没有成功,树甚至不再打印了。
我检查了文件输入,它工作正常。我已经检查了显示功能,它工作正常(但为了以防万一,我会把它放在下面。)而且我已经检查了主要功能,看起来一切正常。问题似乎只是 placeWordInTree 函数的 else 语句初始 if 语句从不执行。
unsigned displayTree(wordnode *currNode) {
unsigned treeLength = 0;
// If the current node is NULL, return 0;
if (currNode == NULL) {
return treeLength;
// If the current node is not null, recursively call displayList on its left and
// right pointers and add their return value to the total tree node count.
} else {
printf("\n%6u\t%s", currNode->count, currNode->word);
treeLength += displayTree(currNode->left);
treeLength += displayTree(currNode->right);
}
return treeLength++;
}
您需要在 placeWordInTree 函数中通过引用传递节点。在 C 中,这是通过将指针传递给要更改的所需变量来实现的。
void placeWordInTree(char *word, wordnode **currNode)
并且在调用函数的时候加上operator的地址
placeWordInTree(wordBuffer, &head);
我正在尝试生成字符串的二叉树。列表中包含的结构是使用以下代码创建的:
typedef struct WordNode {
char *word;
unsigned count;
struct WordNode *left;
struct WordNode *right;
} wordnode;
我在主函数中首先将根节点初始化为 NULL,如下所示:
// The initial struct begins empty.
wordnode *head = NULL;
这是因为初始结构将是给定输入文件中包含的第一个单词。初始化为 NULL 后,我开始处理文件内容:
// While the end of file marker has not been reached...
while (EOF != fscanf(inHandle, BUFFMT, wordBuffer)) {
// Clean the word currently in the wordBuffer.
cleanWord(wordBuffer);
// If the buffer contains an actual word...
if (strlen(wordBuffer) > 0) {
// Add one to the total word count.
totalWords++;
placeWordInTree(wordBuffer, head);
}
}
placeWordInTree函数处理输入文件的每个单词(占第一个空根节点):
void placeWordInTree(char *word, wordnode *currNode) {
// If the current node is empty, initialize it to a newly generated node.
if (currNode == NULL) {
currNode = generateNode(word);
// If the base case has not been satisfied...
} else {
// Store the result of comparing the given word and the current structs word.
int strComp = strcmp(word, currNode->word);
// If the words are equal, add one to the current structs counter.
if (strComp == 0) {
currNode->count++;
// If the given word is less than the struct word, recursively call this function
// with the given node and the current structs left node.
} else if (strComp < 0) {
placeWordInTree(word, currNode->left);
// If the given word is greater than the struct word, recursively call this
// function with the given word and the current structs right node.
} else if (strComp > 0) {
placeWordInTree(word, currNode->right);
}
}
}
这个函数检查当前节点是否为NULL。如果是,它将使用给定单词的 generateNode 函数生成的新结构分配给当前结构。如果当前结构不为空,它将给定字与当前结构字进行比较,然后将计数器加一或尝试递归地将节点放置在当前结构左侧的节点中(如果它小于当前结构字)或正确的结构(如果更多)。
generateNode 函数生成一个这样的节点:
wordnode *generateNode(char *word) {
// Allocates enough memory to hold a new word node.
wordnode *newNode = malloc(sizeof(wordnode));
// Allocates enough memory to store the new nodes given word.
newNode->word = malloc(strlen(word)+1);
// Copies the given word into the new nodes word field.
strncpy(newNode->word, word, strlen(word));
// Sets the new nodes count to 1, and its branches to NULL.
newNode->count = 1;
newNode->left = NULL;
newNode->right = NULL;
// Return the new node.
return newNode;
}
我遇到的问题是程序似乎认为在 placeWordInTree 函数中检查 head 结构时它始终为 NULL。当放置用于调试的 printf 语句时,它告诉我当 placeWordInTree 函数的每个可能结果时,它总是执行代码,就好像 head struct 总是 NULL 一样。更令人困惑的是,当打印树的内容时,head 结构包含的单词始终是输入文件的第一个单词。经过漫长的调试没有成功,树甚至不再打印了。
我检查了文件输入,它工作正常。我已经检查了显示功能,它工作正常(但为了以防万一,我会把它放在下面。)而且我已经检查了主要功能,看起来一切正常。问题似乎只是 placeWordInTree 函数的 else 语句初始 if 语句从不执行。
unsigned displayTree(wordnode *currNode) {
unsigned treeLength = 0;
// If the current node is NULL, return 0;
if (currNode == NULL) {
return treeLength;
// If the current node is not null, recursively call displayList on its left and
// right pointers and add their return value to the total tree node count.
} else {
printf("\n%6u\t%s", currNode->count, currNode->word);
treeLength += displayTree(currNode->left);
treeLength += displayTree(currNode->right);
}
return treeLength++;
}
您需要在 placeWordInTree 函数中通过引用传递节点。在 C 中,这是通过将指针传递给要更改的所需变量来实现的。
void placeWordInTree(char *word, wordnode **currNode)
并且在调用函数的时候加上operator的地址
placeWordInTree(wordBuffer, &head);