Почему мне не распечатать последний узел списка? - PullRequest
0 голосов
/ 04 февраля 2020

Привет, я не могу понять, почему у меня не печатается последний узел списка (только в последний раз). В первый раз, когда я просто печатаю список (и все работает), во-вторых, я собираюсь суммировать все предыдущие номера узла с этим узлом (и это работает, и все узлы печатаются), когда я в последний раз хочу Суммируйте только число перед тем, которое у них есть (если список 1 2 3, его нужно изменить как 1 3 5), в моей программе все работает нормально, но я не печатаю последний узел, поэтому я должен был быть напечатан просто 1 и 3, а не 5. не могу понять, почему, вы можете помочь?

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