fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct link {
  5. struct link *next;
  6. };
  7.  
  8. struct link *link(struct link *next) {
  9. struct link *node = malloc(sizeof *node);
  10. if (node) {
  11. node->next = next;
  12. }
  13. return node;
  14. }
  15.  
  16. struct link *append(struct link ***tail) {
  17. struct link *node = link(**tail);
  18. if (link) {
  19. **tail = link(**tail);
  20. *tail = &(**tail)->next;
  21. }
  22. return node;
  23. }
  24.  
  25. size_t length(struct link *node) {
  26. size_t length = 0;
  27. while (node) {
  28. length += 1;
  29. node = node->next;
  30. }
  31. return length;
  32. }
  33.  
  34. int main() {
  35. struct link *list = NULL;
  36. struct link **tail = &list;
  37.  
  38. if (append(&tail) && append(&tail) && append(&tail)) {
  39. printf("%zu\n", length(list));
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0.02s 1852KB
stdin
Standard input is empty
stdout
3