简体   繁体   English

类型为void *的链表

[英]linked list of type void*

I'm trying to read a text file and store each line in a node of a link list of type void*. 我正在尝试读取文本文件并将每一行存储在类型为void *的链接列表的节点中。 Here's the header file of the list. 这是列表的头文件。

#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

All the linked list functions have been thoroughly tested, so I guess the problem is with how I use it. 所有的链表功能都经过了全面的测试,所以我想问题出在我的使用方式上。 What I want to achieve is the have one line in each node and what I have now is last line in every node. 我要实现的是每个节点中只有一行,而我现在的是每个节点中的最后一行。 I guess it has something to do with the char pointers but have already spent two hours on that without a spectacular breakthrough, so maybe someone could help? 我想这与char指针有关,但是已经花了两个小时而没有取得重大突破,所以也许有人可以帮忙吗? Also the list I use is a modified list as seen 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);

        }

UPDATE: Thank you All. 更新:谢谢大家。

You are creating each node using a single array, temp. 您正在使用单个数组temp创建每个节点。 Every time you read a line, you replace the contents of temp with the last line you read. 每次阅读一行时,都用最后阅读的行替换temp的内容。 That's why you have the last line on every node (you are referring to the same memory location in every node). 这就是为什么每个节点上都有最后一行的原因(每个节点都指向相同的内存位置)。

What you should do, is to allocate memory dynamically for each line using malloc. 您应该做的是使用malloc为每行动态分配内存。 Thus, you should pass the pointer to the newly allocated memory to list_insert_after instead of passing temp. 因此,您应该将指向新分配的内存的指针传递给list_insert_after,而不是传递temp。

Think about this - you have a temporary char array on stack which is getting destroyed upon exiting out of the scope (of else block) and you are inserting pointer to that array into a list. 考虑一下-您在堆栈上有一个临时char数组,该数组在退出( else块的)作用域时会被破坏,并且您正在将指向该数组的指针插入列表中。 So after all, the list will end up having pointers to destroyed/incorrect data. 因此,毕竟,该列表最终将具有指向已损坏/错误数据的指针。 The behavior is undefined. 该行为是不确定的。

You have to allocate memory dynamically for each string (and not forget to clean it up). 您必须为每个字符串动态分配内存(并且不要忘记清理它)。 The strdup can be useful here. strdup在这里可能很有用。 Just don't forget to call free when removing/dropping string from that list. 只是不要忘记从该列表中删除/删除字符串时free通话。

Remove the line: 删除行:

strcpy(temp, mystring); 

And change the line: 并更改行:

current=list_insert_after(current, (void*)temp);

To

current=list_insert_after(current, strdup(mystring));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM