DragonFly On-Line Manual Pages
RBOPENLIST(3) Linux Programmer's Manual RBOPENLIST(3)
NAME
rbopenlist, rbreadlist, rbcloselist - read from a red-black tree
SYNOPSIS
#include <redblack.h>
RBLIST *rbopenlist ( const struct rbtree *rb);
const void *rbreadlist ( RBLIST * rblp);
void rbcloselist ( RBLIST * rblp);
DESCRIPTION
rbopenlist, rbreadlist, rbcloselist provide a simple way to read from a
redblack binary tree created by rbinit(3).
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.
RETURN VALUE
rbopenlist returns a pointer to the new list structure, NULL if there
was insufficient memory to allocate the structure. rbreadlist returns
the next node data pointer in the tree, NULL when there are no more
entries. rbcloselist has no return value.
EXAMPLE
The following program inserts twelve random numbers into a binary tree,
then prints the numbers in order.
#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;
}
SEE ALSO
rbinit(3), rbgen(1), tsearch(3).
GNU May 23, 2000 RBOPENLIST(3)