我正在尝试读取文本文件并将每行存储在类型为void *的链接列表的节点中。 这是列表的头文件。链接列表类型void *
#ifndef LINKEDL
#define LINKEDL
struct node_s {
void *data;
struct node_s *next;
};
struct node_s *node_create(void*);
struct node_s *list_insert_after(struct node_s*, void*);
struct node_s *list_insert_beginning(struct node_s*, void*);
int list_remove(struct node_s*, struct node_s*);
int list_foreach(struct node_s*, int(*)(void*));
int printstring(void *s);
#endif
所有的链接列表功能已经彻底的测试,所以我想这个问题是我如何使用它。我想要实现的是每个节点都有一条线,而我现在拥有的是每个节点的最后一条线。我猜这跟char指针有点关系,但已经花了两个小时没有突破,所以也许有人可以帮忙? 此外,我使用的列表是一个修改列表,如看到here。
if (file == NULL)
{
perror("Error opening file");
}
else
{
char mystring[SIZE];
char temp[SIZE];
list = node_create((void*)mystring);
current = list;
while (fgets(mystring, SIZE, file) != NULL)
{
strcpy(temp, mystring);
printf("%d\t%s",counter++,temp);
current=list_insert_after(current, (void*)temp);
}
fclose(file);
}
更新: 谢谢大家。
你只需要一个字符串变量(MyString的),所以所有的节点将指着这个相同的字符串。您需要为每个新节点指向一个新字符串。 – 2011-02-01 17:15:42