Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int value;
- struct node *next;
- }node;
- node *alloc_node(int value) {
- node *n = malloc(sizeof(struct node));
- if (n == NULL) {
- fprintf(stderr, "Error in malloc\n");
- exit(EXIT_FAILURE);
- }
- n->value = value;
- n->next = NULL;
- return n;
- }
- void insert(node **head, int value) {
- node *newNode = alloc_node(value);
- newNode->next = *head;
- *head = newNode;
- }
- void print_list(node *head) {
- if (head == NULL)
- return;
- printf("%d ", head->value);
- print_list(head->next);
- }
- node *parse_values(char *values) {
- node *head = NULL;
- int value;
- while (sscanf(values, "%d", &value) == 1)
- insert(&head, value);
- return head;
- }
- int main(int argc, char **argv)
- {
- if (argc >= 3) {
- fprintf(stderr, "Usage: <program> <list of values>\n");
- exit(EXIT_FAILURE);
- }
- fprintf(stderr, "Arriva qui1");
- node *head = parse_values(argv[1]);
- fprintf(stderr, "Arriva qui2");
- print_list(head);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement