#include <stdio.h> #include "srtunq.h"
The caller has control over the database through the use of a
SRTTABLE
variable. The subroutines provide for data entry and
retrieval, memory allocation and deallocation.
tbl
before any data are entered or retrieved from that tree.
It assumes that the tag has not been used
to store a tree, and therefore does not attempt to free any such data.
compare
. This subroutine takes two string pointers as arguments.
It returns zero if the strings are the same,
less than zero if the first string should precede the second, and
greater than zero if the second string should precede the first.
Use strcmp(3) if simple lexicographical ordering is desired.
It is confusing at best if different compare
functions are used
when inserting strings into a given tree.
string
if it is already a member of
the table, else NULL.
tbl
so that a tree traversal can be made via srtgets
.
compare
function when they were inserted with srtin.
When the list is exhausted, NULL is returned.
func
to each string in the tree (order
determined by compare
when they were inserted) until the func returns
non-zero, or there are no more strings. If the func
returned
non-zero then that value is returned, otherwise 0 is returned. The (void *)
will always we a NULL pointer.
func
to each string in the tree (order
determined by compare
when they were inserted) until the func returns
non-zero, or there are no more strings. If the func
returned
non-zero then that value is returned, otherwise 0 is returned. The (void *)
is common to all calls the func
, this is usually used to
return a value by reference, keep some state, or may be used to
get to a recursive instance of srtapply2
.
#include <stdio.h> main() { extern int strcmp(); SRTTABLE tree; char buf[80], *p; int i; /* init the tree */ srtinit(&tree); /* add some strings */ while (NULL != fgets(buf, 80, stdin)) /* want the \n terminator */ if (NULL == (p = srtin(&tree, buf, strcmp))) printf("out of memory!\n"); /* init tree for srtgets */ srtgti(&tree); /* print out the strings with srtapply and printf -- can't have * a legal printf % escape in the strings! */ (void) srtapply(&tree, printf); /* use srtgets to print the strings out this time -- keep count */ for (i = 0; NULL != (p = srtgets(&tree)); ++i) printf("string %2d is: %s\n", i, p); printf("there were %d strings\n", i); /* free the database */ srtfree(&tree); }
$Id: srtunq.html,v 6.8 2012/03/21 16:15:05 ksb Exp $