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