If you don't know how to create lists see Adding elements to Eina_List. In this example we also use Stringshare, however it should be possible to understand the code regardless of previous knowledge about it.
Here we have the usual list creation code with a twist, now we are using as data for the list memory that has to be freed later on.
#include <stdio.h>
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
void *list_data;
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition eina_list.c:584
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
EINA_API Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition eina_stringshare.c:606
Type for a generic double linked list.
Definition eina_list.h:318
This time we are going to iterate over our list in a different way:
for(l = list; l; l = eina_list_next(l))
printf(
"%s\n", (
char*)l->
data);
void * data
Pointer to list element payload.
Definition eina_list.h:319
And now we are going to iterate over the list backwards:
for(l = eina_list_last(list); l; l = eina_list_prev(l))
printf("%s\n", (char*)eina_list_data_get(l));
And now we need to free up the memory allocated during creation of the list:
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition eina_list.h:1629
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition eina_stringshare.c:533
- Note
- We don't need to use eina_list_free() since EINA_LIST_FREE takes care of that.
And shut everything down:
return 0;
}
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition eina_main.c:379
The full source code can be found on the examples folder on the eina_list_04.c file.