【发布时间】:2014-01-03 03:29:01
【问题描述】:
我的链接列表有一个小问题。 我用字符串构建了一个链表,它工作得很好。 现在因为我使用 strtok() 来分隔字符串,所以我需要帮助来单独存储结构但保持它们连接。 希望我解释清楚
现在这就是我所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dict_word *word;
typedef struct node *Node;
typedef struct double_linked_list *DLL;
struct dict_word
{
char words[100];
int year[10];
char eng_synonyms[100];
char heb_synonyms[100];
};
struct node
{
word data;
Node *next;
Node *previous;
};
struct double_linked_list
{
Node *head;
Node *last;
};
char *split(char words[100])
{
int i;
char *word=strtok(words, "_#_");
char *year=strtok(NULL, "_#_");; // assigning NULL for previousely where it left off
char *definition=strtok(NULL,"_#_");
char *synonyms=strtok(NULL,"_#_");
i=atoi(year);
printf("%s\n", word);
printf("%i\n",i);
printf("%s\n", definition);
printf("%s\n", synonyms);
return 0;
}
这是我通过只有一个字符串插入节点的功能:
void insert_beginning(char words[99])
{
struct node *var, *temp;
var=(struct node *)malloc(sizeof(struct node)); //explination about the (node *)
strncpy(var->data, words,99);
if (head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
【问题讨论】:
-
因为你有很多字符串要存储,建议为你的结构创建那么多 char* .. 不过需要一些关于你的问题的解释
-
我需要建立一个双链表来存储像这样的字符串:love_#_1986_#_affection_#_ADORE 基本上我需要一个函数来存储每个单词但保持它们之间的连接。就像我需要搜索 ADORE 一样,输出应该是 Found ADORE in the word love
-
我解释清楚了吗?
标签: c string linked-list