//The Last attempt//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct WordNode {
char word;
int line;
struct WordNode* left;
struct WordNode* right;
};
struct WordNode createNode(char word, int line) {
struct WordNode* node=NULL;
node =malloc(sizeof(struct WordNode));
node->word = word;
node->line = line;
node->left = NULL;
node->right = NULL;
return node;
}
struct WordNode insert(struct WordNode* root, char word, int line) {
if (root==NULL) {
return createNode(word, line);
}
int cmp = strcmp(word, root->word);
if (cmp == 0) {
// word already exists in tree, so do nothing
return root;
} else if (cmp < 0) {
root->left = insert(root->left, word, line);
} else {
root->right = insert(root->right, word, line);
}
return root;
}
int main(int argc, char argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>n", argv[0]);
return 1;
}
char filename = argv[1];
FILE *file = fopen("D:TXTFolderText1.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
struct WordNode *root = NULL;
char line[256];
int lineNumber = 1;
while (fgets(line, sizeof(line), file)) {
char word = strtok(line, " nt");
while (word != NULL) {
root = insert(root, word, lineNumber);
word = strtok(NULL, " nt");
}
lineNumber++;
}
fclose(file);
return 0;
}
这是一个程序,它读取一个文本文件并将该文本文件中的唯一单词按字母顺序存储在二叉搜索树中这个程序还在同一二叉搜索树中存储这些存储的唯一单词在c中提到的行索引
似乎有两个函数createNode()
和insert()
的错误,因为它们都应该返回一个结构体,但似乎我不能。
malloc(sizeof(struct WordNode))
分配足够的内存来存储struct WordNode
并返回指向该内存的指针;内存的地址。你把它赋值给struct WordNode*
(一个指向struct WordNode
的指针),如果你想返回它,你需要返回一个指向struct WordNode
的指针。
// v returns a pointer
struct WordNode *createNode(char word, int line) {
// malloc returns a pointer
struct WordNode* node = malloc(sizeof(struct WordNode));
node->word = word;
node->line = line;
node->left = NULL;
node->right = NULL;
// this is a pointer
return node;
}
…在其他地方返回指向WordNode的指针。
您可以返回struct Node
…的副本
struct WordNode createNode(char *word, int line) {
struct WordNode* node=NULL;
node =malloc(sizeof(struct WordNode));
node->word = word;
node->line = line;
node->left = NULL;
node->right = NULL;
// v dereferences the pointer so it refers to the actual memory
return *node;
}
…但是现在你不能检查node == NULL
之类的东西了。
习惯使用指针。
其他问题是你的"文字";是单个字符,char
,但您将其视为字符串。C语言中的字符串是指向字符数组的指针,即char *
。
struct WordNode {
// v pointer
char *word;
int line;
struct WordNode* left;
struct WordNode* right;
};
…在其他地方使用word
必须是char *word
。
您还需要将argv[]声明为字符指针(字符串)数组。
// v array of strings
int main(int argc, char *argv[]) {
strtok
不复制单词,它只是指向原始字符串中的单词。当您存储从strtok
返回的word
时,它指向line
中的内存。line
在每次读取一行时被覆盖,因此存储的单词将被覆盖。
你需要把这个单词复制到新的内存中。使用strdup
.
// line's memory is overwritten each time fgets is called
while (fgets(line, sizeof(line), file)) {
// word points to memory in line
char word = strtok(line, " nt");
while (word != NULL) {
// vvvvvvvvvvvv copy the word from line to new memory
root = insert(root, strdup(word), lineNumber);
// word points to memory in line
word = strtok(NULL, " nt");
}
lineNumber++;
}
双引号字符串中的反斜杠表示以下字符具有特殊含义,如n
表示换行符,t
表示制表符。如果您指的是字面反斜杠,请使用\
。
FILE *file = fopen("D:\TXTFolder\Text1.txt", "r");
从struct函数返回struct ?
简单地返回struct
,就像代码会返回int
一样。
struct WordNode createNode(char word, int line) {
//struct WordNode* node=NULL;
//node =malloc(sizeof(struct WordNode));
//node->word = word;
//node->line = line;
//node->left = NULL;
//node->right = NULL;
struct WordNode node = {.word = word, .line = line, .left = NULL, .roght = NULL};
return node;
}
这是不是一个好主意。最好分配内存并返回指向struct WordNode
的指针。
很少字可以保存在char
中。像'A'
,'I'
,'O'
这样的词,到目前为止,函数更可能接受指向字符的指针(char *
),createNode()
将分配引用的字符串的副本。
对于my_strdup()
,请参阅此
struct WordNode *createNode(const char *word, int line) {
struct WordNode* node = malloc(sizeof node[0]);
if (node) {
node->word = my_strdup(word);
node->line = line;
node->left = NULL;
node->right = NULL;
}
return node;
}