【发布时间】:2017-02-25 23:01:47
【问题描述】:
我正在尝试使用 while 循环将元素添加到双向链表。正在制作节点,但它们都存储相同的单词,这是我正在阅读的文件的最后一个单词。这是我的while循环:
while(fscanf(text, "%s", word) == 1)
{
struct node *temp;
temp = new_node(word); //Creates a new node
temp->prev = cursor; //Cursor represents current position in linked list
temp->next = NULL;
cursor->next = temp;
cursor = temp;
}
光标在while循环开始之前被初始化到列表的头部。
这是我的节点结构:
struct node
{
struct node* prev;
struct word_entry* data;
struct node* next;
};
我的 while 循环有什么问题?为什么它会不断覆盖以前的节点?请,谢谢!
【问题讨论】:
-
错误可能在
new_node()中,您没有向我们展示。 -
temp = new_node(word);-->temp = new_node(strdup(word));
标签: c struct doubly-linked-list