Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将单词输入到链接列表?_C_String_Linked List - Fatal编程技术网

C 如何将单词输入到链接列表?

C 如何将单词输入到链接列表?,c,string,linked-list,C,String,Linked List,我有一个双链接列表,能够在每个节点中保存字符。这就是我为每个节点输入字符的方法 printf("Enter string of characters for the list: "); scanf("%s",s); for(i=0;s[i]!='\0';i++) Insert(s[i],&Header1); 现在,我希望修改列表以在每个节点中存储单词。用户提供的输入是一个句子。如何确保每个单词(用空格分隔)都进入列表的一个节点 while ( sscanf( sentence,

我有一个双链接列表,能够在每个节点中保存字符。这就是我为每个节点输入字符的方法

printf("Enter string of characters for the list: ");
scanf("%s",s);
for(i=0;s[i]!='\0';i++)
    Insert(s[i],&Header1);
现在,我希望修改列表以在每个节点中存储单词。用户提供的输入是一个句子。如何确保每个单词(用空格分隔)都进入列表的一个节点

while ( sscanf( sentence, "%s", &node_value ) == 1 )
{
  //Call to insert into your list goes here
  //Each pass node_value will be the next word
}

注意:您必须按值将
节点\u值
传递到列表中,否则所有值都将是相同的引用

您需要将节点修改为
结构节点{
节点*prev;

char*数据;

节点*下一步;

}


并将
scanf
更改为'
fgets


注意:我已将数据声明为
char*
,因此不能使用strncpy。如果要复制字符串(而不是指定指针),则应
malloc
数据。

您需要告诉我们
Header1
的类型和结构。当然,您必须修改
Insert
函数(因此也可以使用该代码),并在循环中调用
scanf
。或者您可以通过get()函数获取输入(它也将占用空格),然后将字符串存储在节点中(当然,您必须更改节点的结构以获取字符串而不是字符)您使用的Insert中的代码是什么..给出一些详细信息
char *word;
while (NULL != (word = strtok(s, " ."))) {
    Insert(word, &Header1);
}