2.8 Insertion and Deletion [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

These functions insert and delete items in tables. There is also a function for searching a table without modifying it.

The design behind the insertion functions takes into account a couple of important issues:

10. <Table insertion and deletion function prototypes 10> =
void **tbl_probe (struct tbl_table *, void *);
void *tbl_insert (struct tbl_table *, void *);
void *tbl_replace (struct tbl_table *, void *);
void *tbl_delete (struct tbl_table *, const void *);
void *tbl_find (const struct tbl_table *, const void *);

This code is included in 15.

Each of these functions takes a table to manipulate as its first argument and a table item as its second argument, here called table and item, respectively. Both arguments must be non-null in all cases. All but tbl_probe() return a table item or a null pointer.

Exercises:

1. Functions tbl_insert() and tbl_replace() return NULL in two very different situations: an error or successful insertion. Why is this not necessarily a design mistake? [answer]

2. Suggest a reason for disallowing insertion of a null item. [answer]

3. Write generic implementations of tbl_insert() and tbl_replace() in terms of tbl_probe(). [answer]