Reordering Eina_List elements

If you don't know how to create lists see Adding elements to Eina_List.

We start out with code that should be familiar by now:

#include <stdio.h>
#include <Eina.h>
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
Eina_List *list = NULL, *r_list;
Eina_List *l;
Eina_Iterator *itr;
void *list_data;
eina_init();
list = eina_list_append(list, "caprica");
list = eina_list_append(list, "sagitarius");
list = eina_list_append(list, "aerilon");
list = eina_list_append(list, "gemenon");

You can move elements around in a list using eina_list_move() or using eina_list_promote_list() and eina_list_demote_list() which move a list node to the head and end of the list respectively:

list = eina_list_promote_list(list, eina_list_nth_list(list, 2));
list = eina_list_demote_list(list, eina_list_nth_list(list, 2));

Removing elements from a list can be done with ease:

list = eina_list_remove(list, "sagitarius");

To replace an element in the list it is not necessary to remove it and then add with the new value, it is possible to just change the value of a node:

l = eina_list_data_find_list(list, "aerilon");
eina_list_data_set(l, "aquarius");

We will now take a peek to see if the list still has the right number of elements:

printf("size: %d\n", eina_list_count(list));

Now that the list is in alphabetical order let's create a copy of it in reverse order and print every element to see if worked as expected:

r_list = eina_list_reverse_clone(list);
itr = eina_list_iterator_new(r_list);
EINA_ITERATOR_FOREACH(itr, list_data)
printf("%s\n", (char*)list_data);
eina_iterator_free(itr);

Note
  Always remember to free your iterators when done using them.

And as always release memory and shutdown eina before ending:

eina_list_free(list);
eina_list_free(r_list);
eina_shutdown();
return 0;
}

The full source code can be found on the examples folder on the eina_list_03.c file.