C program Linked List

Hi everyone,

I'm new to C and I'm stuck with a problem.
I'm building a linked list that gets a full string and divides it into 4 parts after the '_#_' characters.

Ex: trade_#_2009_#_invest_#_PROFIT
It separates to: trade 2009 invest PROFIT

So i managed to use strtok() and atoi to get the char 2009 into an int.
So basically what I'm searching to do is to Create a linked list that stores all the parsed words of every list in one single structure.

I would like to show you what I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #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[99])
{
    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;
}


The code below is a code I did to generate the same thing but on a singly linked list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

void insert_beginning(char words[99]);
void insert_end(char words[99]);
char insert_after(char words[99], char loc[99]);
char delete_from_end();
char delete_from_middle(char words[99]);
void display();
struct node *merge(struct node *head_one, struct node *head_two);




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


What I want to know is how to build my insert_begining with what I showed you.
Appreciate the help
Your insert_beginning does access two global variables:
1
2
struct node * head;
struct node * tail;

You apparently want to replace those variables with one (preferably not global) variable of type struct double_linked_list, e.g. replace head=var with foo.head=var.
Topic archived. No new replies allowed.