fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct list_t
  5. {
  6. int value;
  7. struct list_t * next;
  8. } list;
  9.  
  10. void insert(list **, int);
  11. void print(list *);
  12. void destroy(list *);
  13.  
  14. int main()
  15. {
  16. list * mylist = NULL;
  17.  
  18. insert(&mylist, 10);
  19. insert(&mylist, 20);
  20.  
  21. print(mylist);
  22. destroy(mylist);
  23. return 0;
  24. }
  25.  
  26. void insert(list ** head, int value)
  27. {
  28. list * new_node = NULL;
  29. new_node = (list *)malloc(sizeof(list));
  30.  
  31. new_node->value = value;
  32. new_node->next = NULL;
  33.  
  34. if(*head == NULL)
  35. {
  36. *head = new_node;
  37. }
  38. else
  39. {
  40. list * cur = *head;
  41. while(cur->next != NULL)
  42. {
  43. cur = cur->next;
  44. }
  45. cur->next = new_node;
  46. }
  47. }
  48.  
  49. void print(list * head)
  50. {
  51. while(head != NULL)
  52. {
  53. printf("%d\t", head->value);
  54. head = head->next;
  55. }
  56. }
  57.  
  58. void destroy(list * head)
  59. {
  60. while(head != NULL)
  61. {
  62. list * tmp = head->next;
  63. free(head);
  64. head = tmp;
  65. }
  66. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
10	20