3

I have problems when adding nodes to a singly linked list, for some reason the

if(current->next == NULL)

gets skipped when the list is empty, and this causes a seg fault when I display the list... I'm really confused as to why it's getting skipped any ideas?

 void addToList(node** head, char name[30], int groupSize, boolean inRest){
     //create new node
     node *newNode = (node*)malloc(sizeof(node));

     if (newNode == NULL) {
         fprintf(stderr, "Unable to allocate memory for new node\n");
         exit(-1);
     }

     InitNodeInfo(newNode, name, groupSize, inRest);
     node *current = head;
     //check for first insertion
     if (current->next == NULL) {
         current->next = newNode;
         printf("added at beginning\n");
     } else {
         //else loop through the list and find the last
         //node, insert next to it
           while (current->next != NULL) {
               current = current->next;
           }
           current->next = newNode;
           printf("added later\n");
           *head = current;
     }

     if (debug == FALSE) {      //debug mode...
         printf("Pushed the value %d on to the stack\n", groupSize);
     } else {
         printf("\n\nATTENTION : Group cannot be added, the name entered already exists!\n\n");
     }
}
6
  • mind the type of head and current. Maybe you want node *current = *head;. Then what happens when you add it at the beginning? At present you print only, but do not reassign/move head. Actually, you do it in the other case... Commented Oct 19, 2015 at 21:52
  • @Pynchia I receive a seg. fault at the if statement. Commented Oct 19, 2015 at 21:52
  • I can see that. Read my comment again :) Commented Oct 19, 2015 at 21:53
  • @Pynchia I must be missing something lol, I have no idea what you're referring to. I'm sorry, could you elaborate? Commented Oct 19, 2015 at 21:57
  • just tell me: when the list is empty, does head point to NULL or there is always a dummy element at the beginning? (i.e. head points to a dummy element) Commented Oct 19, 2015 at 21:59

1 Answer 1

1

Assuming an empty list has head == NULL

this should work (I have no means of trying it now)

 void addToList(node** head, char name[30], int groupSize, boolean inRest){
    //create new node
    node *newNode = (node*)malloc(sizeof(node));

    if(newNode == NULL){
        fprintf(stderr, "Unable to allocate memory for new node\n");
        exit(-1);
    }

    InitNodeInfo(newNode, name, groupSize, inRest);
    node *current = *head;
    //check for first insertion
    if(current == NULL){
        // make head point to the new node
        *head = newNode;
        printf("added at beginning\n");
    }
    else
    {
        //else loop through the list and find the last
        //node, insert next to it
          while (current->next != NULL) {
            current = current->next;
          }
          current->next = newNode;
          printf("appended\n");
    }
    // mark the element as the last one
    newNode->next = NULL;

    if (debug == FALSE) {//debug mode...
         printf("Pushed the value %d on to the stack\n", groupSize);
    }
    else{
        printf("\n\nATTENTION : Group cannot be added, the name entered already exists!\n\n");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

you are welcome :). Pointers to pointers tend to be tricky and it's easy to trip
A more apt message instead of 'added at the beginning' would be 'first node'. Nothing wrong with your code. This is merely a suggestion.

Your Answer

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