5. Putting it all together: Using one single object for both the list and data handling
So now that we have split the node and data logic, let's join them again! :-)
In a singly-linked list, the entire list is accessed from the "head node".
(Once assigned, the "head node" should never be modified until deletion.)
To access other nodes, a "current node" (a.k.a. "cursor") is used.
Both are needed for most list operations.
With our approach, using a set of data handling functions, we also need a structure of callback:
/* Struct of callbacks to deal with node data */
typedef struct node_data_fp_t
{
data_add_t add;
data_set_t set;
data_get_t get;
data_print_t print;
data_del_t del;
data_cleanup_t cleanup;
} node_data_cb_t;
So, instead of declaring both "head node" and the "current node" in a calling program, both nodes are kept in one single llwfp_t struct.
And instead of passing a callback for data handling to each call to a node operation, the struct of callbacks is used.
So the final object used from outside the library looks like this:
typedef struct llwfp_t
{
node_t *head; /* First node in list */
node_t *current; /* Current node in list */
node_data_cb_t *df; /* The struct of callbacks */
} llwfp_t;
%%% TODO EXAMPLE 5 %%%