0

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?

1
  • The insertion code looks ok to me. One thing though. If you are using typedef there is no need to write struct Cons every time. You can simply write Cons. As for parsing the input the since you have not given much details about it in code, I can't say much about it. Commented Mar 28, 2015 at 3:54

2 Answers 2

1

Change the code like this

   Cons *insert_node(Cons *head_pointer, int data){
        Cons *new = (struct Cons*) malloc(sizeof(struct Cons));
        Cons *current = head_pointer;
        new->head = data;
        new->tail = NULL;

         if (head_pointer== NULL){
             head_pointer->tail = new;
         }
      else
       {

            while (current->tail != NULL){
               current = current->tail;
       }
           current->tail = new;
    }
 //free(current);
  //current = NULL; 
    return head_poiner;
  }

in a main() function call like;

    main()
     {
      ..........
      ..........
      while(fgets()!=NULL){

       head_pointer=insert_node(head_pointer,data);
     .........
     .........

     }
Sign up to request clarification or add additional context in comments.

5 Comments

but then how would I use what insert_node returns to actually insert the node? Could you update your code to show an example or an implementation
@cp101020304 Each time of calling the insert_node() function, you just pass the head_pointer as call by value, when your adding the nodes, it will not affect the original pointer in the main() function , that is why we return the pointer.
I don't quite understand, can you give an example?
@cp101020304 - consider you have a nodes like when call the insert_node function |1|->|2|->|3|->NULL and you just send the pointer of 1 to insert_node function this is call by value calling, so that will go as a local variable for that function, then you are adding the value 4 like |1|->|2|->|3|->|4|->NULL, it will not affect the value of the main() function, to do that we are return the value.
So lets say I have an int that I want to insert into the linked list. What would I do after the ` head_pointer=insert_node(head_pointer,data);` line in the code? Or does that automatically add the int to the linked list for me?
1

This is my code I have experimented.

    # include <stdio.h>
     # include <stdlib.h>
    struct node
    {
    int data;
    struct node *link;
   };
   struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    /*  if the existing list is empty then insert a new node as the
     *  starting node */
    if(p==NULL)
    {
            if((p=(struct node *)malloc(sizeof(struct node)))==NULL)
            {
                    perror("Error");
                    exit(0);
            }
            p-> data = n;
            p-> link = p; /*  makes the pointer pointing to itself because it
                              is a circular list*/
    }
    else
    {
            temp = p;
            /*  traverses the existing list to get the pointer to the last node of
             *     it */
            while (temp-> link != p)
                    temp = temp-> link;
            if((temp-> link = (struct node *)malloc(sizeof(struct node)))==NULL)
            {
                    perror("Error\n");
                    exit(0);
            }
            temp = temp-> link;
            temp-> data = n;
            temp-> link = p;
    }
    return p;
   }
    void printlist ( struct node *p )
    {
    struct node *temp;
    temp = p;
    printf("The data values in the list are\n");
    if(p!= NULL)
    {
            do
            {
                    printf("%d\t",temp->data);
                    temp=temp->link;
            } while (temp!= p);
            printf("\n");
    }
             else
            printf("The list is empty\n");
      }
  void main()
    {
    int n;
    int x;
    struct node *start = NULL ;
    char buf[BUFSIZ];
    while(fgets(buf,BUFSIZ,stdin)!=NULL){
            x=atoi(buf);
            start = insert ( start, x );
    }
    printlist ( start );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.