fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <assert.h>
  5.  
  6. struct size_t_elem {
  7. size_t lu;
  8. struct size_t_elem *next;
  9. };
  10.  
  11. /* List structure requiring manual bookkeeping for size */
  12. struct size_t_list {
  13. uint32_t size;
  14. const struct size_t_elem *list;
  15. };
  16.  
  17. struct size_t_elem **size_t_list_end(struct size_t_elem **size_t_elem) {
  18. assert(size_t_elem);
  19. while (*size_t_elem)
  20. size_t_elem = &size_t_elem[0]->next;
  21. return size_t_elem;
  22. }
  23.  
  24. struct size_t_elem **size_t_list_prepend(struct size_t_elem **size_t_elem,
  25. const size_t lu) {
  26. struct size_t_elem *x = malloc(sizeof *x);
  27. assert(size_t_elem);
  28. if (!x)
  29. return NULL;
  30. x->lu = lu;
  31. x->next = *size_t_elem;
  32. *size_t_elem = x;
  33. return &x->next;
  34. }
  35.  
  36. struct size_t_elem **size_t_list_append(struct size_t_elem **p,
  37. const size_t lu) {
  38. return size_t_list_prepend(size_t_list_end(p), lu);
  39. }
  40.  
  41. void size_t_list_push(uint32_t *ll_n,
  42. struct size_t_elem ***ll_root,
  43. const size_t lu) {
  44. (*ll_n)++;
  45. *ll_root = size_t_list_append(*ll_root, lu);
  46. }
  47.  
  48. int main(void) {
  49. struct size_t_elem *full_ll = NULL;
  50. struct size_t_elem **ll_cur_ptr = &full_ll, *iter;
  51.  
  52. struct size_t_list *ll = malloc(sizeof *ll);
  53.  
  54. const size_t l[] = {5, 6, 10, 44};
  55. size_t i;
  56. for(i=0; i<sizeof l/sizeof l[0]; i++)
  57. size_t_list_push(&ll->size, &ll_cur_ptr, l[i]);
  58.  
  59. for (iter = (struct size_t_elem *)ll->list, i=0; iter != NULL; iter = iter->next, i++)
  60. printf("ll->list[%lu] = %lu\n", i, iter->lu);
  61.  
  62. return EXIT_SUCCESS;
  63. }
  64.  
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
Standard output is empty