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

typedef struct node_s {
    int value;
    struct node_s *next;
} node;

typedef struct {
    node *first, *last;
} list;


void *xmalloc(int size)
{
    void *p;
    p = malloc(size);
    if (!p) {
        fprintf(stderr, "Could not allocate dynamic memory\n");
        exit(1);
    }
    return p;
}

int str2int(const char *s)
{
    char c;
    int sign = 1;
    int res = 0;

    if (*s == '-') {
        sign = -1;
        s++;
    }
    for (; *s; s++) {
        res *= 10;
        c = *s;
        if (c < '0' || c > '9') {
            fprintf(stderr, "Could not convert string to integer\n");
            exit(2);
        }
        res += c - '0';
    }
    return sign * res;
}

void list_init(list *l)
{
    l->first = NULL;
    l->last = NULL;
}

void node_add(list *l, int value)
{
    node *tmp;
    tmp = xmalloc(sizeof(node));
    tmp->value = value;
    tmp->next = NULL;
    if (!l->first)
        l->first = tmp;
    else
        l->last->next = tmp;
    l->last = tmp;
}

void node_print(const list *l)
{
    node *p;
    for (p = l->first; p; p = p->next)
        printf("%d ", p->value);
    printf("\n");
}

void node_clean(list *l)
{
    node *p, *tmp;
    for (p = l->first; p; p = tmp) {
        tmp = p->next;
        free(p);
    }
    list_init(l);
}

int main(int argc, char **argv)
{
    int i;
    list l;
    list_init(&l);

    for (i = 1; i < argc; i++)
        node_add(&l, str2int(argv[i]));

    node_print(&l);
    node_clean(&l);
    return 0;
}
