Как изменить, чтобы добавить в конец списка - PullRequest
0 голосов
/ 17 января 2020

Я работаю со списками и не могу понять, как я должен изменить код, чтобы символы добавлялись в конец списка, теперь с этим кодом я добавляю их в начало списка.

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

typedef struct node{
    char character;
    struct node *next;
} node_t;

node_t *createNode(char ch){
    node_t *result = malloc(sizeof(node_t));
    result->character = ch;
    result->next = NULL;
    return result;
}

int main (){

    node_t *head=NULL, *temp;
    int i;
    char ch;

    for (i=0; i < MAX; i++){
        printf("Write a character: \n");
        scanf(" ");
        scanf("%c", &ch);
        temp = createNode(ch);
        temp->next = head;
        head = temp;
    }

    while (temp != NULL) {
        printf("%c ", temp->character);
        temp = temp->next;
    }

    printf ("\n"); 

    return 0;
}
...