0

Hi I cannot understand why I do not get printed the last node of the list (just the LAST time). The first time I'm just printing the list (and everything works), the second I'm going to sum all the previous numbers of a node to that node (and it works and all nodes are printed) the last time I want to sum just the number before of the one they have (if the list is 1 2 3 it needs to be modified as 1 3 5), in my program everything is working fine but i do not get printed the last node so i would be printed just 1 and 3 not the 5. cannot understand why, can you help?

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int val;
    struct node * next;
} node_t;

int main () {

    int num, m=0;

    printf("How many numbers you want to gimme: ");
    scanf("%d", &num);

    node_t *head = NULL;
    head = (node_t *)malloc(sizeof(node_t));
    head->next = NULL;

    node_t * current = head;

    while (current->next != NULL){
        current = current->next;
    }

    while (num > 0){
        num--;
        current -> next = (node_t *)malloc(sizeof(node_t));
        printf("Inserisci un valore: ");
        scanf("%d", &current->next->val);
        current = current->next;
        current->next = NULL;
    }

    current = head->next;

    while (current != NULL){
        printf("%d ", current-> val);
        current = current->next;
    }

    printf("\nI'm going to modify the list :D \n");

    current = head->next;

    while (current->next != NULL){
        current->next->val += current->val;
        current = current->next;
    }

    current = head->next;

    while (current != NULL){
        printf("%d ", current->val);
        current = current -> next;
    }

    printf("\nI'm going to modify the list once again :D \n");

    current = head->next;

    while (current->next != NULL){
        current->next->val += ((current->val)-m);
        m = current->val-m;
        current = current->next;
    }

    current = head->next;

    while (current->next != NULL){
        printf ("%d ", current->val);
        current = current->next;
    }

    printf("\n");

    return 0;
}
awwww
  • 23
  • 5
  • In your last `while` loop you are printing nodes as long as the "next" item is not NULL. You should print until the current item is not NULL. I.e., `while (current != NULL)`. Not to mention, if the list were empty, your `while (current->next != NULL)` would cause a null pointer exception. Your middle `while` loop also doesn't consider the case when `current` is NULL, so that needs fixing as well. It should be `while (current != NULL)` and check `if (current->next != NULL)` before attempting `current->next->val` access. – lurker Feb 03 '20 at 21:23
  • Thanks for your help m8 :) – awwww Feb 03 '20 at 22:29
  • @lurker could you please explain when should i use "current->next != NULL" and when "current != NULL" what are the differences btw those 2 expressions ? – awwww Feb 03 '20 at 23:55
  • You don't want to reference `current->next` without knowing `current` is not NULL. You'd be derefencing a null pointer. – lurker Feb 04 '20 at 02:53

0 Answers0