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)
{
Eina_List *list = NULL, *r_list;
void *list_data;
list = eina_list_append(list, "caprica");
list = eina_list_append(list, "sagitarius");
list = eina_list_append(list, "aerilon");
list = eina_list_append(list, "gemenon");
Eina Utility library.
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition eina_list.c:530
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
structure of an iterator
Definition eina_iterator.h:159
Type for a generic double linked list.
Definition eina_list.h:321

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 respectevely:

list = eina_list_demote_list(list, eina_list_nth_list(list, 2));
Eina_List * eina_list_nth_list(const Eina_List *list, unsigned int n)
Get the nth member's list node in a list.
Definition eina_list.c:927
Eina_List * eina_list_promote_list(Eina_List *list, Eina_List *move_list)
Move the specified data to the head of the list.
Definition eina_list.c:775
Eina_List * eina_list_demote_list(Eina_List *list, Eina_List *move_list)
Move the specified data to the tail of the list.
Definition eina_list.c:818

Removing elements from a list can be done with ease:

list = eina_list_remove(list, "sagitarius");
Eina_List * eina_list_remove(Eina_List *list, const void *data)
Remove the first instance of the specified data from the given list.
Definition eina_list.c:708

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");
static void * eina_list_data_set(Eina_List *list, const void *data)
Set the list node data member.
Eina_List * eina_list_data_find_list(const Eina_List *list, const void *data)
Find a member of a list and return the list node containing that member.
Definition eina_list.c:900

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));
static unsigned int eina_list_count(const Eina_List *list)
Get the count of the number of items in a 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);
#define EINA_ITERATOR_FOREACH(itr, data)
Macro to iterate over all elements easily.
Definition eina_iterator.h:332
void eina_iterator_free(Eina_Iterator *iterator)
Free an iterator.
Definition eina_iterator.c:96
Eina_Iterator * eina_list_iterator_new(const Eina_List *list)
Returned a new iterator associated to a list.
Definition eina_list.c:1409
Eina_List * eina_list_reverse_clone(const Eina_List *list)
Clone (copy) all the elements in the list in reverse order.
Definition eina_list.c:991
Note
Always remember to free your iterators when done using them.

And as always release memory and shutdown eina before ending:

eina_list_free(r_list);
return 0;
}
Eina_List * eina_list_free(Eina_List *list)
Free an entire list and all the nodes, ignoring the data contained.
Definition eina_list.c:754
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315

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