changeset 11:8b09943f1a70

Fixed return value of list_next and list_prev
author Eris Caffee <discordia@eldalin.com>
date Fri, 28 Sep 2012 19:37:10 -0500
parents 68f85ffc6029
children d359966ed8de
files src/list.c src/rbtree_test.c
diffstat 2 files changed, 96 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/src/list.c	Fri Sep 28 18:24:53 2012 -0500
     1.2 +++ b/src/list.c	Fri Sep 28 19:37:10 2012 -0500
     1.3 @@ -225,7 +225,10 @@
     1.4        return NULL;
     1.5  
     1.6     it->curr = it->curr->next;
     1.7 -   return it->curr;
     1.8 +
     1.9 +   if (it->curr)
    1.10 +      return it;
    1.11 +   return NULL;
    1.12     }
    1.13  
    1.14  ////////////////////////////////////////////////////////////////////////////////
    1.15 @@ -237,7 +240,10 @@
    1.16        return NULL;
    1.17  
    1.18     it->curr = it->curr->prev;
    1.19 -   return it->curr;
    1.20 +
    1.21 +   if (it->curr)
    1.22 +      return it;
    1.23 +   return NULL;
    1.24     }
    1.25  
    1.26  ////////////////////////////////////////////////////////////////////////////////
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/rbtree_test.c	Fri Sep 28 19:37:10 2012 -0500
     2.3 @@ -0,0 +1,88 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <stdbool.h>
     2.7 +
     2.8 +#include "rbtree.h"
     2.9 +
    2.10 +void print_val(void * data)
    2.11 +   {
    2.12 +   printf("%d", *((int *) data));
    2.13 +   }
    2.14 +
    2.15 +bool le(void * a, void * b)
    2.16 +   {
    2.17 +   return *((int *) a) <= *((int *) b);
    2.18 +   }
    2.19 +
    2.20 +bool eq(void * a, void * b)
    2.21 +   {
    2.22 +   return *((int *) a) == *((int *) b);
    2.23 +   }
    2.24 +
    2.25 +#define MAX_NUMS 100
    2.26 +
    2.27 +int main (int argc, char ** argv)
    2.28 +   {
    2.29 +   int sorted[100] = {
    2.30 +   35005211, 42999170, 84353895, 135497281, 137806862, 149798315, 184803526, 233665123, 278722862, 294702567,
    2.31 +   304089172, 336465782, 356426808, 412776091, 424238335, 468703135, 491705403, 511702305, 521595368, 572660336,
    2.32 +   596516649, 608413784, 610515434, 628175011, 635723058, 709393584, 719885386, 749241873, 752392754, 756898537,
    2.33 +   760313750, 783368690, 805750846, 846930886, 855636226, 859484421, 861021530, 939819582, 943947739, 945117276,
    2.34 +   982906996, 1025202362, 1059961393, 1100661313, 1101513929, 1102520059, 1125898167, 1129566413, 1131176229, 1141616124,
    2.35 +   1159126505, 1189641421, 1264095060, 1303455736, 1315634022, 1350490027, 1365180540, 1369133069, 1374344043, 1411549676,
    2.36 +   1424268980, 1433925857, 1469348094, 1474612399, 1477171087, 1540383426, 1548233367, 1585990364, 1632621729, 1649760492,
    2.37 +   1653377373, 1656478042, 1681692777, 1714636915, 1726956429, 1734575198, 1749698586, 1780695788, 1801979802, 1804289383,
    2.38 +   1827336327, 1843993368, 1889947178, 1911759956, 1914544919, 1918502651, 1937477084, 1956297539, 1957747793, 1967513926,
    2.39 +   1973594324, 1984210012, 1998898814, 2001100545, 2038664370, 2044897763, 2053999932, 2084420925, 2089018456, 2145174067,
    2.40 +   };
    2.41 +
    2.42 +   struct rbtree * rbtree = rbtree_new(le, eq);
    2.43 +   if (NULL == rbtree)
    2.44 +      {
    2.45 +      perror("rbtree_new");
    2.46 +      exit(EXIT_FAILURE);
    2.47 +      }
    2.48 +
    2.49 +   for (int i = 0; i < MAX_NUMS; ++i)
    2.50 +      {
    2.51 +      int * ip = malloc(sizeof(int));
    2.52 +      if (NULL == ip)
    2.53 +	 {
    2.54 +	 perror("malloc");
    2.55 +	 exit(EXIT_FAILURE);
    2.56 +	 }
    2.57 +      *ip = rand();
    2.58 +      // Be sure to set MAX_NUMS = 100 if you want to use the sorted array
    2.59 +      //      *ip = sorted[i];
    2.60 +      int * ip2;
    2.61 +      if (NULL == (ip2 = rbtree_insert(rbtree, ip)))
    2.62 +	 {
    2.63 +	 printf("rbtree_insert failed\n");
    2.64 +	 free(ip);
    2.65 +	 }
    2.66 +      printf("inserting %d\n", *ip2);
    2.67 +      }
    2.68 +
    2.69 +
    2.70 +   // print the tree structure
    2.71 +   rbtree_dump(rbtree, print_val);
    2.72 +   fprintf(stderr, "black depth is %u\n", rbtree_black_depth(rbtree));
    2.73 +
    2.74 +   // Print the tree in value order
    2.75 +   struct rbtree_iterator * it;
    2.76 +   struct rbtree_iterator * next;
    2.77 +   for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it))
    2.78 +      printf("%d\n", *((int *) rbtree_value(it)));
    2.79 +   free(it);
    2.80 +
    2.81 +
    2.82 +   // Free the whole tree
    2.83 +   for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it))
    2.84 +      free(rbtree_value(it));
    2.85 +   free(it);
    2.86 +   
    2.87 +
    2.88 +   rbtree_delete(rbtree);
    2.89 +
    2.90 +   exit(EXIT_SUCCESS);
    2.91 +   }