tree is a list :P
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 47 63adb261de90
parent 46 4b9e1ac40246
child 48 b94d657a9acb
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 <mbroeker@largo.homelinux.org>
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;