int compare_function (void *p1, void *p2) { int v1 = (int) p1; int v2 = (int) p2; if (v1 < v2) { return -1; } if (v1 > v2) { return 1; } return 0; } int pos; void *item; s_list_t *list; // initialize the list struct s_list_init(&list); // add an item to the end item = (void *) 0; s_list_add(list, item, -1); // add an item to the beginning item = (void *) 1; s_list_add(list, item, 0); // add an item to position 1 item = (void *) 2; s_list_add(list, item, 1); // add an item to the end item = (void *) 3; s_list_add(list, item, -1); // find the the item with compare function item = (void *) 2; item = s_list_find(list, item, compare_function); // get the items, and print them // output: 1 2 0 3 for (pos = 0; !s_list_eol(list, pos); pos++) { item = (void *) s_list_get(list, pos); printf("%d ", (int) item; } // remove items while (!s_list_eol(list, 0)) { s_list_remove(list, 0); } // uninitialize the list s_list_uninit(list);
Data Structures | |
| struct | s_list_node_s |
| list node struct More... | |
| struct | s_list_s |
| list struct More... | |
Functions | |
| int | s_list_init (s_list_t **li) |
| initializes the list struct | |
| int | s_list_uninit (s_list_t *li) |
| uninitializes the list struct | |
| int | s_list_eol (s_list_t *li, int i) |
| query if i'nth element exists | |
| void * | s_list_get (s_list_t *li, int pos) |
| return the element at position | |
| int | s_list_remove (s_list_t *li, int pos) |
| removes the element at position | |
| int | s_list_add (s_list_t *li, void *el, int pos) |
| adds the element at position | |
| void * | s_list_find (s_list_t *list, void *node, int(*cmp_func)(void *, void *)) |
| search the node at list, with the given compare function | |
| int | s_list_get_pos (s_list_t *list, void *node) |
| return the position of node | |
| int s_list_add | ( | s_list_t * | li, | |
| void * | el, | |||
| int | pos | |||
| ) |
adds the element at position
| *li | - the list | |
| *el | - element | |
| pos | - position (-1 means the end) |
| int s_list_eol | ( | s_list_t * | li, | |
| int | i | |||
| ) |
query if i'nth element exists
| *li | - the list | |
| i | - position |
| void* s_list_find | ( | s_list_t * | list, | |
| void * | node, | |||
| int(*)(void *, void *) | cmp_func | |||
| ) |
search the node at list, with the given compare function
| *list | - the list | |
| *node | - node to be matched | |
| cmp_func | - compare function. compare function must return -1, 0, 1 for less than, equal to, and greater than |
| void* s_list_get | ( | s_list_t * | li, | |
| int | pos | |||
| ) |
return the element at position
| *li | - the list | |
| pos | - position |
| int s_list_get_pos | ( | s_list_t * | list, | |
| void * | node | |||
| ) |
return the position of node
| *list | - the list | |
| *node | - element |
| int s_list_init | ( | s_list_t ** | li | ) |
initializes the list struct
| **li | - address of the pointer to list struct |
| int s_list_remove | ( | s_list_t * | li, | |
| int | pos | |||
| ) |
removes the element at position
| *li | - the list | |
| pos | - position |
| int s_list_uninit | ( | s_list_t * | li | ) |
uninitializes the list struct
| *li | - the list |
1.4.7