author | Markus Bröker<broeker.markus@googlemail.com> |
Sun, 22 Oct 2017 22:55:27 +0200 | |
changeset 172 | 43ae72f88d06 |
parent 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* tree.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <time.h> |
|
9 |
||
74
829976007e62
getrandom macro fixed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
61
diff
changeset
|
10 |
#define GETRANDOM(max) (1+(int)((float)(max)*rand()/RAND_MAX+1.0)) |
0 | 11 |
|
12 |
struct T { |
|
13 |
int data; |
|
14 |
struct T *next; |
|
15 |
}; |
|
16 |
||
17 |
typedef struct T T; |
|
18 |
||
19 |
T *make_list (int elements, int rand_max) |
|
20 |
{ |
|
21 |
int i; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
22 |
|
47 | 23 |
T *t, *first = NULL; |
0 | 24 |
|
25 |
srand (time (NULL)); |
|
26 |
||
47 | 27 |
if ((t = malloc (sizeof (T))) == NULL) { |
0 | 28 |
perror ("MALLOC"); |
29 |
return first; |
|
30 |
} |
|
31 |
||
32 |
t->data = GETRANDOM (rand_max); |
|
33 |
t->next = NULL; |
|
34 |
||
35 |
first = t; |
|
36 |
||
37 |
for (i = 1; i < elements; i++) { |
|
47 | 38 |
if ((t->next = malloc (sizeof (T))) == NULL) |
0 | 39 |
break; |
47 | 40 |
t->next->data = GETRANDOM (rand_max); |
41 |
t = t->next; |
|
0 | 42 |
} |
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
43 |
|
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
44 |
t->next = NULL; |
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
45 |
|
0 | 46 |
return first; |
47 |
} |
|
48 |
||
49 |
int main (int argc, char **argv) |
|
50 |
{ |
|
47 | 51 |
T *t, *next; |
0 | 52 |
|
53 |
if (argc != 3) { |
|
61
4b4c97f179da
Lazy BNF / EBNF Help Messages
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
54 |
printf ("Usage: %s <elements> <rand_max>\n", argv[0]); |
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
55 |
return EXIT_FAILURE; |
0 | 56 |
} |
57 |
||
58 |
t = make_list (atoi (argv[1]), atoi (argv[2])); |
|
59 |
||
60 |
while (t) { |
|
61 |
printf ("%d\n", t->data); |
|
47 | 62 |
next = t->next; |
0 | 63 |
free (t); |
47 | 64 |
t = next; |
0 | 65 |
}; |
66 |
||
67 |
return EXIT_SUCCESS; |
|
68 |
} |