# HG changeset patch # User Eris Caffee # Date 1348879030 18000 # Node ID 8b09943f1a70c81cf9acb2d85b7d727f18db835c # Parent 68f85ffc6029be17e582bb3a51a6d0a042ed28bf Fixed return value of list_next and list_prev diff -r 68f85ffc6029 -r 8b09943f1a70 src/list.c --- a/src/list.c Fri Sep 28 18:24:53 2012 -0500 +++ b/src/list.c Fri Sep 28 19:37:10 2012 -0500 @@ -225,7 +225,10 @@ return NULL; it->curr = it->curr->next; - return it->curr; + + if (it->curr) + return it; + return NULL; } //////////////////////////////////////////////////////////////////////////////// @@ -237,7 +240,10 @@ return NULL; it->curr = it->curr->prev; - return it->curr; + + if (it->curr) + return it; + return NULL; } //////////////////////////////////////////////////////////////////////////////// diff -r 68f85ffc6029 -r 8b09943f1a70 src/rbtree_test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rbtree_test.c Fri Sep 28 19:37:10 2012 -0500 @@ -0,0 +1,88 @@ +#include +#include +#include + +#include "rbtree.h" + +void print_val(void * data) + { + printf("%d", *((int *) data)); + } + +bool le(void * a, void * b) + { + return *((int *) a) <= *((int *) b); + } + +bool eq(void * a, void * b) + { + return *((int *) a) == *((int *) b); + } + +#define MAX_NUMS 100 + +int main (int argc, char ** argv) + { + int sorted[100] = { + 35005211, 42999170, 84353895, 135497281, 137806862, 149798315, 184803526, 233665123, 278722862, 294702567, + 304089172, 336465782, 356426808, 412776091, 424238335, 468703135, 491705403, 511702305, 521595368, 572660336, + 596516649, 608413784, 610515434, 628175011, 635723058, 709393584, 719885386, 749241873, 752392754, 756898537, + 760313750, 783368690, 805750846, 846930886, 855636226, 859484421, 861021530, 939819582, 943947739, 945117276, + 982906996, 1025202362, 1059961393, 1100661313, 1101513929, 1102520059, 1125898167, 1129566413, 1131176229, 1141616124, + 1159126505, 1189641421, 1264095060, 1303455736, 1315634022, 1350490027, 1365180540, 1369133069, 1374344043, 1411549676, + 1424268980, 1433925857, 1469348094, 1474612399, 1477171087, 1540383426, 1548233367, 1585990364, 1632621729, 1649760492, + 1653377373, 1656478042, 1681692777, 1714636915, 1726956429, 1734575198, 1749698586, 1780695788, 1801979802, 1804289383, + 1827336327, 1843993368, 1889947178, 1911759956, 1914544919, 1918502651, 1937477084, 1956297539, 1957747793, 1967513926, + 1973594324, 1984210012, 1998898814, 2001100545, 2038664370, 2044897763, 2053999932, 2084420925, 2089018456, 2145174067, + }; + + struct rbtree * rbtree = rbtree_new(le, eq); + if (NULL == rbtree) + { + perror("rbtree_new"); + exit(EXIT_FAILURE); + } + + for (int i = 0; i < MAX_NUMS; ++i) + { + int * ip = malloc(sizeof(int)); + if (NULL == ip) + { + perror("malloc"); + exit(EXIT_FAILURE); + } + *ip = rand(); + // Be sure to set MAX_NUMS = 100 if you want to use the sorted array + // *ip = sorted[i]; + int * ip2; + if (NULL == (ip2 = rbtree_insert(rbtree, ip))) + { + printf("rbtree_insert failed\n"); + free(ip); + } + printf("inserting %d\n", *ip2); + } + + + // print the tree structure + rbtree_dump(rbtree, print_val); + fprintf(stderr, "black depth is %u\n", rbtree_black_depth(rbtree)); + + // Print the tree in value order + struct rbtree_iterator * it; + struct rbtree_iterator * next; + for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it)) + printf("%d\n", *((int *) rbtree_value(it))); + free(it); + + + // Free the whole tree + for (next = it = rbtree_begin(rbtree); NULL != next; next = rbtree_next(it)) + free(rbtree_value(it)); + free(it); + + + rbtree_delete(rbtree); + + exit(EXIT_SUCCESS); + }