是什么让它在标记化后插入新元素时不起作用
what makes it not working while inserting new elements after tokenizing
我开始研究 C 以获得总体思路。我创建了一个链表结构,我可以毫无问题地插入新元素。列表的想法是当已经插入相同的元素时它会增加计数,我顺便从文件中读取单词。
typedef struct Node
{
char* word;
int count;
struct Node *next;
struct Node *head;
struct Node *current;
} Node;
然后,当我尝试在下面的代码中使用此列表时,它会添加单词但不会增加计数。它把这个词当作一个新元素(当我多次硬编码其他东西时它会增加,比如 insertFirst("testString"))
char* pch;
pch = strtok(line," ,.-()\r\n\t");
while (pch != NULL)
{
printf("%s-",pch);
int i = 0;
for(; pch[i]; i++){
pch[i] = tolower(pch[i]);
}
insertFirst(r,(char*) pch,1); // inserts but doesn't increment the count;
pch = strtok (NULL, " ,.-()\r\n\t");
}
上面的代码逐行读取文件,删除所有符号、空格、换行等。我想将单词放入列表中 "r"。我确信 insertFirst 方法没有问题,因为它在没有 tokenize
的情况下运行良好
//INSERT METHOD-WRONG ONE
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1; //
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
strcpy(&link->word, &word);
link->count = count;
link->next = r->head;
r->head = link;
}
}
感谢评论,下面的代码非常有用
//WORKING INSERT METHOD
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1;
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
link->word = malloc(strlen(word)+1);
strcpy(link->word, word);
link->count = count;
link->next = r->head;
r->head = link;
}
}
这是vadim_hr解决
的解决方案
//WORKING INSERT METHOD
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1;
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
link->word = malloc(strlen(word)+1);
strcpy(link->word, word);
link->count = count;
link->next = r->head;
r->head = link;
}
}
我开始研究 C 以获得总体思路。我创建了一个链表结构,我可以毫无问题地插入新元素。列表的想法是当已经插入相同的元素时它会增加计数,我顺便从文件中读取单词。
typedef struct Node
{
char* word;
int count;
struct Node *next;
struct Node *head;
struct Node *current;
} Node;
然后,当我尝试在下面的代码中使用此列表时,它会添加单词但不会增加计数。它把这个词当作一个新元素(当我多次硬编码其他东西时它会增加,比如 insertFirst("testString"))
char* pch;
pch = strtok(line," ,.-()\r\n\t");
while (pch != NULL)
{
printf("%s-",pch);
int i = 0;
for(; pch[i]; i++){
pch[i] = tolower(pch[i]);
}
insertFirst(r,(char*) pch,1); // inserts but doesn't increment the count;
pch = strtok (NULL, " ,.-()\r\n\t");
}
上面的代码逐行读取文件,删除所有符号、空格、换行等。我想将单词放入列表中 "r"。我确信 insertFirst 方法没有问题,因为它在没有 tokenize
的情况下运行良好//INSERT METHOD-WRONG ONE
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1; //
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
strcpy(&link->word, &word);
link->count = count;
link->next = r->head;
r->head = link;
}
}
感谢评论,下面的代码非常有用
//WORKING INSERT METHOD
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1;
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
link->word = malloc(strlen(word)+1);
strcpy(link->word, word);
link->count = count;
link->next = r->head;
r->head = link;
}
}
这是vadim_hr解决
的解决方案//WORKING INSERT METHOD
void insertFirst(Node* r, char* word, int count){
if(find(r, word)){
struct Node* temp = find(r,word);
temp->count += 1;
}
else{
struct Node *link = (struct Node*) malloc(sizeof(struct Node));
link->word = malloc(strlen(word)+1);
strcpy(link->word, word);
link->count = count;
link->next = r->head;
r->head = link;
}
}