fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tag_List {
  5. struct tag_List *next;
  6. int val;
  7. } List;
  8.  
  9. void init(List *);
  10. void add(List *, int);
  11.  
  12. int main()
  13. {
  14. List ob_root;
  15. List *p;
  16.  
  17. init(&ob_root);
  18. add(&ob_root, 1);
  19. add(&ob_root, 2);
  20. add(&ob_root, 3);
  21.  
  22. for (p = &ob_root; NULL != p->next; p = p->next) {
  23. printf("%d\n", p->val);
  24. }
  25. return 0;
  26. }
  27.  
  28. void init(List * p)
  29. {
  30. p->next = NULL;
  31. p->val = 0;
  32. }
  33.  
  34. void add(List * pob_root, int v)
  35. {
  36. List *p;
  37. for (p = pob_root; NULL != p->next; p = p->next) {
  38. ;
  39. }
  40. p->val = v;
  41. p->next = malloc(sizeof(*p));
  42. init(p->next);
  43. }
  44.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
1
2
3