# HG changeset patch # User Markus Bröker # Date 1239878952 -7200 # Node ID 63adb261de90f88409c2b3dcfa098d94cfdcc016 # Parent 4b9e1ac4024666bc901ef782ed46a896ca4f5d7c tree is a list :P * anyway, some changes here -> a struct T consumes sizeof(struct T) of memory -> actual wasn't really needed here. thrown away. committer: Markus Bröker diff --git a/tree.c b/tree.c --- a/tree.c +++ b/tree.c @@ -20,13 +20,11 @@ { int i; - T *t; - T *actual; - T *first = NULL; + T *t, *first = NULL; srand (time (NULL)); - if ((t = malloc (sizeof (T) + 1)) == NULL) { + if ((t = malloc (sizeof (T))) == NULL) { perror ("MALLOC"); return first; } @@ -37,19 +35,18 @@ first = t; for (i = 1; i < elements; i++) { - if ((actual = malloc (sizeof (T) + 1)) == NULL) + if ((t->next = malloc (sizeof (T))) == NULL) break; - actual->data = GETRANDOM (rand_max); - actual->next = NULL; - t->next = actual; - t = actual; + t->next->data = GETRANDOM (rand_max); + t->next->next = NULL; + t = t->next; } return first; } int main (int argc, char **argv) { - T *t, *actual; + T *t, *next; if (argc != 3) { printf ("Usage: %s elements rand_max\n", argv[0]); @@ -60,9 +57,9 @@ while (t) { printf ("%d\n", t->data); - actual = t->next; + next = t->next; free (t); - t = actual; + t = next; }; return EXIT_SUCCESS;