I'm using strtok() to parse an input, convert the string into an int and then insert this int value into a linked list all in a while loop.
This is what I'm trying to do (I haven't written the code explicitly but I'm planning to do do something as follows):
while(fgets(&string,LMAX,fp) != NULL){
//get first token using strtok
//convert to int
//insert into linked list
while (token != NULL){
//get next token in the line
//do the same as above
}
}
I already have written a function that is supposed to insert a node into the linked list and it is as follows:
void insert_node(struct Cons *head_pointer, int data){
struct Cons *new = (struct Cons*) malloc(sizeof(struct Cons));
struct Cons *current = head_pointer;
new->head = data;
new->tail = NULL;
if (head_pointer->tail == NULL){
head_pointer->tail = new;
}
else
{
while (current->tail != NULL){
current = current->tail;
}
current->tail = new;
}
free(current);
current = NULL;
}
The struct definition is also as follows:
typedef int element_t;
typedef
struct Cons {
element_t head;
struct Cons* tail;
} Cons;
Can anyone suggest how I can go about doing this?
typedefthere is no need to writestruct Consevery time. You can simply writeCons. As for parsing the input the since you have not given much details about it in code, I can't say much about it.