author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:47:18 +0200 | |
changeset 27 | 81a574d60c15 |
parent 9 | c3fecc82ade6 |
child 29 | 7abf6146898e |
permissions | -rw-r--r-- |
0 | 1 |
/** |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
2 |
* test/demos/tree.c |
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 |
||
10 |
#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.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 |
|
0 | 23 |
T *t; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
24 |
|
0 | 25 |
T *actual; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
26 |
|
0 | 27 |
T *first = NULL; |
28 |
||
29 |
srand (time (NULL)); |
|
30 |
||
31 |
if ((t = malloc (sizeof (T) + 1)) == NULL) { |
|
32 |
perror ("MALLOC"); |
|
33 |
return first; |
|
34 |
} |
|
35 |
||
36 |
t->data = GETRANDOM (rand_max); |
|
37 |
t->next = NULL; |
|
38 |
||
39 |
first = t; |
|
40 |
||
41 |
for (i = 1; i < elements; i++) { |
|
42 |
if ((actual = malloc (sizeof (T) + 1)) == NULL) |
|
43 |
break; |
|
44 |
actual->data = GETRANDOM (rand_max); |
|
45 |
actual->next = NULL; |
|
46 |
t->next = actual; |
|
47 |
t = actual; |
|
48 |
} |
|
49 |
return first; |
|
50 |
} |
|
51 |
||
52 |
int main (int argc, char **argv) |
|
53 |
{ |
|
54 |
T *t, *actual; |
|
55 |
||
56 |
if (argc != 3) { |
|
57 |
printf ("Usage: %s elements rand_max\n", argv[0]); |
|
58 |
return EXIT_SUCCESS; |
|
59 |
} |
|
60 |
||
61 |
t = make_list (atoi (argv[1]), atoi (argv[2])); |
|
62 |
||
63 |
while (t) { |
|
64 |
printf ("%d\n", t->data); |
|
65 |
actual = t->next; |
|
66 |
free (t); |
|
67 |
t = actual; |
|
68 |
}; |
|
69 |
||
70 |
return EXIT_SUCCESS; |
|
71 |
} |