author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:49:11 +0200 | |
changeset 36 | f52a9deddfc0 |
parent 29 | 7abf6146898e |
child 47 | 63adb261de90 |
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; |
24 |
T *actual; |
|
25 |
T *first = NULL; |
|
26 |
||
27 |
srand (time (NULL)); |
|
28 |
||
29 |
if ((t = malloc (sizeof (T) + 1)) == NULL) { |
|
30 |
perror ("MALLOC"); |
|
31 |
return first; |
|
32 |
} |
|
33 |
||
34 |
t->data = GETRANDOM (rand_max); |
|
35 |
t->next = NULL; |
|
36 |
||
37 |
first = t; |
|
38 |
||
39 |
for (i = 1; i < elements; i++) { |
|
40 |
if ((actual = malloc (sizeof (T) + 1)) == NULL) |
|
41 |
break; |
|
42 |
actual->data = GETRANDOM (rand_max); |
|
43 |
actual->next = NULL; |
|
44 |
t->next = actual; |
|
45 |
t = actual; |
|
46 |
} |
|
47 |
return first; |
|
48 |
} |
|
49 |
||
50 |
int main (int argc, char **argv) |
|
51 |
{ |
|
52 |
T *t, *actual; |
|
53 |
||
54 |
if (argc != 3) { |
|
55 |
printf ("Usage: %s elements rand_max\n", argv[0]); |
|
56 |
return EXIT_SUCCESS; |
|
57 |
} |
|
58 |
||
59 |
t = make_list (atoi (argv[1]), atoi (argv[2])); |
|
60 |
||
61 |
while (t) { |
|
62 |
printf ("%d\n", t->data); |
|
63 |
actual = t->next; |
|
64 |
free (t); |
|
65 |
t = actual; |
|
66 |
}; |
|
67 |
||
68 |
return EXIT_SUCCESS; |
|
69 |
} |