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, "sagittarius");
list = eina_list_append(list, "aerilon");
list = eina_list_append(list, "gemenon");
Eina.h
Eina Utility library.
eina_list_append
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_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
_Eina_Iterator
structure of an iterator
Definition: eina_iterator.h:159
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:318

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));
eina_list_nth_list
EINA_API Eina_List * eina_list_nth_list(const Eina_List *list, unsigned int n)
Gets the nth member's list node in a list.
Definition: eina_list.c:1006
eina_list_demote_list
EINA_API Eina_List * eina_list_demote_list(Eina_List *list, Eina_List *move_list)
Moves the specified data to the tail of the list.
Definition: eina_list.c:889
eina_list_promote_list
EINA_API Eina_List * eina_list_promote_list(Eina_List *list, Eina_List *move_list)
Moves the specified data to the head of the list.
Definition: eina_list.c:845

Removing elements from a list can be done with ease:

list = eina_list_remove(list, "sagittarius");
eina_list_remove
EINA_API Eina_List * eina_list_remove(Eina_List *list, const void *data)
Removes the first instance of the specified data from the given list.
Definition: eina_list.c:773

To replace an element in the list it is not necessary to remove it and then re-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");
eina_list_data_set
static void * eina_list_data_set(Eina_List *list, const void *data)
Sets the list node data member.
eina_list_data_find_list
EINA_API Eina_List * eina_list_data_find_list(const Eina_List *list, const void *data)
Finds a member of a list and returns it as a list node.
Definition: eina_list.c:975

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));
eina_list_count
static unsigned int eina_list_count(const Eina_List *list)
Gets 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);
eina_iterator_free(itr);
eina_iterator_free
EINA_API void eina_iterator_free(Eina_Iterator *iterator)
Frees an iterator.
Definition: eina_iterator.c:98
EINA_ITERATOR_FOREACH
#define EINA_ITERATOR_FOREACH(itr, data)
Definition for the macro to iterate over all elements easily.
Definition: eina_iterator.h:448
eina_list_reverse_clone
EINA_API Eina_List * eina_list_reverse_clone(const Eina_List *list)
Clones (copies) all the elements in the list in reverse order.
Definition: eina_list.c:1072
eina_list_iterator_new
EINA_API Eina_Iterator * eina_list_iterator_new(const Eina_List *list)
Returns a new iterator associated with a list.
Definition: eina_list.c:1574

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;
}
eina_list_free
EINA_API Eina_List * eina_list_free(Eina_List *list)
Frees an entire list and all the nodes, ignoring the data contained.
Definition: eina_list.c:823
eina_shutdown
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_03.c file.