Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5. int value;
  6. struct node *next;
  7. }node;
  8.  
  9. node *alloc_node(int value) {
  10. node *n = malloc(sizeof(struct node));
  11. if (n == NULL) {
  12. fprintf(stderr, "Error in malloc\n");
  13. exit(EXIT_FAILURE);
  14. }
  15. n->value = value;
  16. n->next = NULL;
  17. return n;
  18. }
  19.  
  20. void insert(node **head, int value) {
  21. node *newNode = alloc_node(value);
  22. newNode->next = *head;
  23. *head = newNode;
  24. }
  25.  
  26. void print_list(node *head) {
  27. if (head == NULL)
  28. return;
  29. printf("%d ", head->value);
  30. print_list(head->next);
  31. }
  32.  
  33. node *parse_values(char *values) {
  34. node *head = NULL;
  35. int value;
  36. while (sscanf(values, "%d", &value) == 1)
  37. insert(&head, value);
  38. return head;
  39. }
  40.  
  41. int main(int argc, char **argv)
  42. {
  43. if (argc >= 3) {
  44. fprintf(stderr, "Usage: <program> <list of values>\n");
  45. exit(EXIT_FAILURE);
  46. }
  47.  
  48. fprintf(stderr, "Arriva qui1");
  49.  
  50. node *head = parse_values(argv[1]);
  51. fprintf(stderr, "Arriva qui2");
  52. print_list(head);
  53.  
  54. return EXIT_SUCCESS;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement