【发布时间】:2021-08-10 23:17:10
【问题描述】:
我正在尝试使用链接列表从文本文件中提取文本。到目前为止我的代码:
#include <stdlib.h>
#include <string.h>
#define N 20
struct node
{
char * str;
struct node * node;
};
typedef struct node Node;
typedef Node * WordList;
typedef Node * Node_ptr;
void addtext(WordList * lst_ptr);
void report(WordList words);
Node * createnode(char *str);
int main(void)
{
WordList words = NULL;
addtext(&words);
report(words);
return 0;
}
Node * createnode(char *str)
{
Node_ptr newnode_ptr ;
newnode_ptr = malloc(sizeof (Node));
newnode_ptr -> str = NULL;
newnode_ptr -> node = NULL;
return newnode_ptr;
}
void addtext(WordList * lst_ptr)
{
FILE *fileinput;
char word[N];
char *pnt;
if ((fileinput = fopen("test.txt", "rt"))==NULL)
{
printf("Cannot open file. Please check if the file is in the right directory and you are giving the correct name.");
exit(EXIT_FAILURE);
}
while (fscanf(fileinput, "%19s", word) == 1)
{
Node * newnode = createnode(word);
if (*lst_ptr==NULL)
{
newnode->node = *lst_ptr;
*lst_ptr = newnode;
}
else
{
Node *iter;
for (iter = *lst_ptr; iter -> node != NULL; iter = iter -> node);
iter -> node = newnode;
}
pnt = strtok (word," ");
while (pnt != NULL)
{
newnode->str = pnt;
pnt = strtok (NULL, " ");
}
}
fclose(fileinput);
return ;
}
void report(WordList words)
{
if (words == NULL)
{
printf("\n") ;
return ;
}
printf("%s\n",words -> str);
report(words -> node);
return;
}
我制作的文本文件包含以下内容:
Hello
Bye
但我的程序的输出是:
Bye
(random characters)
意味着它打印最后一个单词。我的猜测是,当我调用报告时,它指向列表的最后一个元素,并从那里继续执行与文本文件中单词数量一样多的次数。 我还在报告功能上尝试了不同的方法:
void report(WordList words)
{
Node * iter;
for (iter = words; iter!=NULL; iter = iter->node)
{
printf("%s\n", iter->str);
}
return;
}
但现在它会打印 \n 直到循环结束。我错过了一些简单的事情还是我犯了一个严重的错误?
【问题讨论】:
标签: c struct linked-list nodes