Content-type: text/html
#include <redblack.h> RBLIST *rbopenlist ( const struct rbtree *rb); const void *rbreadlist ( RBLIST * rblp); void rbcloselist ( RBLIST * rblp);
rbopenlist initialises the list and returns a RBLIST pointer that is used in subsequent calls to rbreadlist and rbcloselist.
rbreadlist returns a pointer to the node data. Each subsequent call returns the next node in the order specified by the tree.
rbcloselist simply frees up the memory used to allocate the RBLIST data pointer.
#include <redblack.h> #include <stdlib.h> #include <stdio.h> void *xmalloc(unsigned n) { void *p; p = malloc(n); if(p) return p; fprintf(stderr, "insufficient memory\n"); exit(1); } int compare(const void *pa, const void *pb, const void *config) { if(*(int *)pa < *(int *)pb) return -1; if(*(int *)pa > *(int *)pb) return 1; return 0; } int main() { int i, *ptr; void *val; struct rbtree *rb; RBLIST *rblist; srand(getpid()); if ((rb=rbinit(compare, NULL))==NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } for (i = 0; i < 12; i++) { ptr = (int *)xmalloc(sizeof(int)); *ptr = rand()&0xff; val = rbsearch((void *)ptr, rb); if(val == NULL) exit(1); } if ((rblist=rbopenlist(rb))==NULL) { fprintf(stderr, "insufficient memory from rbopenlist()\n"); exit(1); } while((val=rbreadlist(rblist))) { printf("%6d\n", *(int *)val); } rbcloselist(rblist); rbdestroy(rb); return 0; }