fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum {
  5. MINVAL = 0,
  6. MAXVAL = 500,
  7. NELEMS = MAXVAL - MINVAL + 1,
  8. NUMBERS = 5,
  9. ZERO_BASED = 1
  10. };
  11.  
  12. struct node {
  13. int val;
  14. struct node *next;
  15. };
  16.  
  17. struct node *new(int v, struct node *np)
  18. {
  19. static struct node mem[NUMBERS];
  20. static int n;
  21.  
  22. mem[n].val = v;
  23. mem[n].next = np;
  24. return mem + n++;
  25. }
  26.  
  27. int main(void)
  28. {
  29. static struct node *a[NELEMS];
  30. struct node *np;
  31. int i, r;
  32.  
  33. for (i = 0; i < NUMBERS; i++) {
  34. printf(" %d", (r = rand() / (RAND_MAX / NELEMS)) + 1);
  35. a[r] = new(i, a[r]);
  36. }
  37. puts("");
  38. for (i = 0; i < NELEMS; i++)
  39. for (np = a[i]; np; np = np->next)
  40. printf(" %d", np->val + !ZERO_BASED);
  41. puts("");
  42. return 0;
  43. }
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
 421 198 393 401 457
 1 2 3 0 4