I'm having a small issue here with my linked list. I built a linked list with strings and it worked perfectly. Now since i'm using strtok() to separate the string I need help on storing the struct separately but keeping them connected. Hope i explained it well

for now here's what i've got:

#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;
 }

and this is my function to insert node by having only one string:

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;
}
 }

any help?

any questions?? any ideas??

strtok() will destroy the original character array by enbedding '\0' each time strtok() is called. So if you need the original character array then the function that calls strtok() first needs to duplicate the string.

#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* words_dup = malloc(strlen(words)+1);
strcpy(words_dup,words);
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);

// now restore words
strcpy(words,words_dup);
free(words_dup);
return 0;
 }

Hey thanks for the answer but at a certain point i'll need to search for a definition and get the word it relates.
I'll give you an example. if lets say the program asks to input a word to the dictionary and the user inputs (word)_#_(year)_#_(definition)_#_(diff synonyms)
After creating my linked list I need to have a function called search_definition and gives me back the word it relates to. so basically for every string i enter to my dictionary (Double Linked List) i need to store the elements of it.

(word)#(year)#(definition)#(diff synonyms)

Huh? Why would you want to input that string??? It would be helpful if you posted the actual text of the first couple lines of the file.

i'll need to search for a definition and get the word

What do you do with multiple words that have the same definition?

I would add definition to the structure so that you don't have to search the original string each time you want to use it.

 struct dict_word
 {
char words[100];
char definition[100];
int year[10];
char eng_synonyms[100];
char heb_synonyms[100];
 };

Now just split the original string only once, immediately after it's read from the file.

sorry for that i must've forgotten.
Well basically if the definition relates to more than one word i need to print all the words they relate to.

I'm now a little confused about what it is that you need help with. Do you need help reading the strings from the file, separating them, saving individual parts in the structure? Or do you need help in searching the linked list?

I need to seperate the string first into 4, then store them on my linked list.
I posted an example of insert_begining above but I don't know how to do it.
I also need to keep the full string as well because when I'll have a function to search a year or definition or synonym i'll have it connected to the source.
See what I mean?

As you read each line of the file create a node of dict_word and fill in the struct with the information from the line. After that you can add the new node to the head of the linked list.

 struct dict_word
 {
    char orig_string[100];
    char word[40];
    char definition[100];
    int year;
    char eng_synonyms[100];
    char heb_synonyms[100];
 };

 void insert_beginning(char words[100])
 {
    struct dict_word* word = malloc(sizeof(strucgt dict_word));
    strcpy(word->orig_string,words);
    strcpy(word->word,strtok(words, "_#_"));
    word->year=atoi(strtok(NULL, "_#_"));
    strcpy(word->definition, strtok(NULL,"_#_"));
    strcpy(word->eng_synonyms,strtok(NULL,"_#_")); // this line may be wrong
    if (head==NULL)
    {
        head=word;
        head->previous=NULL;
        head->next=NULL;
        last=head;
    }
    else
    {
         word->next = head;
         head->previous = word;
         word->previous = NULL;
         head = word;
    }

 }

head is not declared though.

You used it on line 5 of your original post so I thought it was already declared somewhere. If not, then pass it as a parameter to the funciton

struct dict_word
{
    char orig_string[100];
    char word[40];
    char definition[100];
    int year;
    char eng_synonyms[100];
    char heb_synonyms[100];
    struct dict_word* next;
    struct dict_word* previous;
};

void insert_beginning(struct dict_word** head, struct dict_word** last, char words[100])
{
    struct dict_word* word = malloc(sizeof(struct dict_word));
    strcpy(word->orig_string, words);
    strcpy(word->word, strtok(words, "_#_"));
    word->year = atoi(strtok(NULL, "_#_"));
    strcpy(word->definition, strtok(NULL, "_#_"));
    strcpy(word->eng_synonyms, strtok(NULL, "_#_")); // this line may be wrong
    if (*head == NULL)
    {
        *head = word;
        (*head)->previous = NULL;
        (*head)->next = NULL;
        *last = *head;
    }
    else
    {
        word->next = *head;
        (*head)->previous = word;
        word->previous = NULL;
        *head = word;
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.