wget2  1.0.0
Double linked list

Functions

void * wget_list_append (wget_list_t **list, const void *data, size_t size)
 
void * wget_list_prepend (wget_list_t **list, const void *data, size_t size)
 
void wget_list_remove (wget_list_t **list, void *elem)
 
void * wget_list_getfirst (const wget_list_t *list)
 
void * wget_list_getlast (const wget_list_t *list)
 
void * wget_list_getnext (const void *elem)
 
int wget_list_browse (const wget_list_t *list, wget_list_browse_t browse, void *context)
 
void wget_list_free (wget_list_t **list)
 

Detailed Description

Double linked lists provide fast insertion, removal and iteration in either direction.

Each element has pointers to the next and the previous element.
Iteration can be done by calling the wget_list_browse() function, so the list structure doesn't need to be exposed.

This datatype is used by the Wget tool to implement the job queue (append and remove).

See wget_list_append() for an example on how to use lists.

Function Documentation

◆ wget_list_append()

void* wget_list_append ( wget_list_t **  list,
const void *  data,
size_t  size 
)
Parameters
[in]listPointer to Pointer to a double linked list
[in]dataPointer to data to be inserted
[in]sizeSize of data in bytes
Returns
Pointer to the new element

Append an element to the end of the list.
size bytes at data will be copied and appended to the list.

A pointer to the new element will be returned.

Note
The returned pointer must be freed by wget_list_remove() or implicitly by wget_list_free().

Example:

wget_list_t *list = NULL;
struct mystruct mydata1 = { .x = 1, .y = 25 };
struct mystruct mydata2 = { .x = 5, .y = 99 };
struct mystruct *data;
wget_list_append(&list, &mydata1, sizeof(mydata1)); // append mydata1 to list
wget_list_append(&list, &mydata2, sizeof(mydata2)); // append mydata2 to list
data = wget_list_getfirst(list);
printf("data=(%d,%d)\n", data->x, data->y); // prints 'data=(1,25)'
wget_list_remove(&list, data);
data = wget_list_getfirst(list);
printf("data=(%d,%d)\n", data->x, data->y); // prints 'data=(5,99)'

◆ wget_list_prepend()

void* wget_list_prepend ( wget_list_t **  list,
const void *  data,
size_t  size 
)
Parameters
[in]listPointer to Pointer to a double linked list
[in]dataPointer to data to be inserted
[in]sizeSize of data in bytes
Returns
Pointer to the new element

Insert an entry at the beginning of the list. size bytes at data will be copied and prepended to the list.

A pointer to the new element will be returned. It must be freed by wget_list_remove() or implicitely by wget_list_free().

◆ wget_list_remove()

void wget_list_remove ( wget_list_t **  list,
void *  elem 
)
Parameters
[in]listPointer to Pointer to a double linked list
[in]elemPointer to a list element returned by wget_list_append() or wget_list_prepend()

Remove an element from the list.

◆ wget_list_getfirst()

void* wget_list_getfirst ( const wget_list_t *  list)
Parameters
[in]listPointer to a double linked list
Returns
Pointer to the first element of the list or NULL if the list is empty

Get the first element of a list.

◆ wget_list_getlast()

void* wget_list_getlast ( const wget_list_t *  list)
Parameters
[in]listPointer to a double linked list
Returns
Pointer to the last element of the list or NULL if the list is empty

Get the last element of a list.

◆ wget_list_getnext()

void* wget_list_getnext ( const void *  elem)
Parameters
[in]elemPointer to an element of a linked list
Returns
Pointer to the next element of the list or NULL if the list is empty

Get the next element of a list.

◆ wget_list_browse()

int wget_list_browse ( const wget_list_t *  list,
wget_list_browse_t  browse,
void *  context 
)
Parameters
[in]listPointer to a double linked list
[in]browsePointer to callback function which is called for every element in the list. If the callback functions returns a value not equal to zero, browsing is stopped and this value will be returned by wget_list_browse.
[in]contextThe context handle that will be passed to the callback function
Returns
The return value of the last call to the browse function or -1 if list is NULL (empty)

Iterate through all entries of the list and call the function browse for each.

// assume that list contains C strings.
wget_list_t *list = NULL;
static int print_elem(void *context, const char *elem)
{
printf("%s\n",elem);
return 0;
}
void dump(WGET_LIST *list)
{
wget_list_browse(list, (wget_list_browse_t)print_elem, NULL);
}

◆ wget_list_free()

void wget_list_free ( wget_list_t **  list)
Parameters
[in]listPointer to Pointer to a double linked list

Freeing the list and it's entry.