#include <inttypes.h>
#include <malloc.h>
#include <stdio.h>
struct list {
int64_t value;
struct list *next;
};
struct list *node_create(int64_t value) {
struct list *node = malloc(sizeof(struct list));
node->value = value;
node->next = NULL;
return node;
}
void list_add_last(struct list **list,
int64_t value) {
if (!(*list)) {
*list = node_create(value);
} else {
struct list *node = *list;
while (node->next) {
node = node->next;
}
node->next = node_create(value);
}
}
void print(const struct list *list) {
if (list) {
printf("%" PRId64, list->value);
if (list->next)
printf(" -> ");
print(list->next);
}
}
int main() {
struct list *list = NULL;
list_add_last(&list, 5);
list_add_last(&list, 2);
list_add_last(&list, 3);
list_add_last(&list, -5);
print(list);
return 0;
}
Comments