fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list {
  5. int data;
  6. struct list *next;
  7. } List, *PList;
  8.  
  9. struct list * list_insert (int data, struct list * root)
  10. {
  11. struct list *p = (struct list *) malloc(sizeof (struct list));
  12. p->data = data;
  13. p->next = NULL;
  14. if (!root)
  15. root = p;
  16. else
  17. {
  18. struct list *iter = root;
  19. while (iter->next)
  20. iter = iter->next;
  21. iter->next = p;
  22. }
  23. return root;
  24. }
  25.  
  26. void list_print (struct list * root)
  27. {
  28. while (root)
  29. {
  30. printf ("%d\n", root->data);
  31. root = root->next;
  32. }
  33. }
  34.  
  35. void list_destroy (struct list * root)
  36. {
  37. if (root != NULL)
  38. {
  39. list_destroy (root->next);
  40. free (root);
  41. }
  42. }
  43.  
  44. int main (int argc, char *argv[])
  45. {
  46. struct list *root = NULL;
  47. root = list_insert (1, root);
  48. root = list_insert (2, root);
  49. root = list_insert (3, root);
  50.  
  51. list_print(root);
  52. list_destroy (root);
  53. list_print(root);
  54.  
  55. int p;
  56. scanf("%d", &p);
  57. return 0;
  58. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
1
2
3
138235920
138235936
0