libEtPan! API
Prev | Chapter 2. Tools and datatypes | Next

# List

    #include <libetpan/libetpan.h>

    typedef struct clist_s clist;

    typedef clistcell clistiter;

clist() is a list of cells. Each cell of the list contains one element. This element is a pointer. An iterator (clistiter) is a pointer to an element of the list. With an iterator, we can get the previous element of the list, the next element of the list and the content of the element.

## clist_new and clist_free

    clist * clist_new(void);

    void clist_free(clist *);

clist_new() allocates a new empty list and returns it.

clist_free() frees the entire list with its cells.

Example 2-5. clist creation

    #include <libetpan/libetpan.h>

    int main(void)
    {
      clist * list;

      list = clist_new();
      if (list == NULL)
        goto err;

      r = clist_append(list, "foo-bar");
      if (r < 0)

      clist_free(list);

      exit(EXIT_SUCCESS);

     free:
      clist_free(list);
     err:
      exit(EXIT_FAILURE);
    }

## clist_isempty and clist_count

    int clist_isempty(clist *);

    int clist_count(clist *);

clist_isempty() returns 1 if the list is empty, else it is 0. Complexity is O(1).

clist_count() returns the number of elements in the list. Complexity is O(1).

## running through clist

    clistiter * clist_begin(clist *);

    clistiter * clist_end(clist *);

    clistiter * clist_next(clistiter *);

    clistiter * clist_previous(clistiter *);

    void * clist_content(clistiter *);

    void * clist_nth_data(clist * lst, int index);

    clistiter * clist_nth(clist * lst, int index);

clist_begin() returns an iterator to the first element of the list. Complexity is O(1).

clist_end() returns an iterator to the last element of the list. Complexity is O(1).

clist_next() returns an iterator to the next element of the list. Complexity is O(1).

clist_previous() returns an iterator to the previous element of the list. Complexity is O(1).

clist_content() returns the element contained in the cell pointed by the iterator in the list. Complexity is O(1).

clist_nth() returns an iterator on the index-th element of the list. Complexity is O(n).

clist_nth_data() returns the index-th element of the list. Complexity is O(n).

Example 2-6. displaying content of clist

    #include <libetpan/libetpan.h>

    int main(void)
    {
      clist * list;
      clistiter * iter;

      list = build_string_list();
      if (list == NULL)
        goto err;

      for(iter = clist_begin(list) ; iter != NULL ; iter =
         clist_next(iter)) {
        char * str;

        str = clist_content(iter);
        printf("%s\n", str);
      }

      clist_free(list);

      exit(EXIT_SUCCESS);

     free:
      clist_free(list);
     err:
      exit(EXIT_FAILURE);
    }

## clist modification

    int clist_prepend(clist *, void *);

    int clist_append(clist *, void *);

    int clist_insert_before(clist *, clistiter *, void *);

    int clist_insert_after(clist *, clistiter *, void *);

    clistiter * clist_delete(clist *, clistiter *);

clist_prepend() adds an element at the beginning of the list. Returns 0 on sucess, -1 on error. Complexity is O(1).

clist_append() adds an element at the end of the list. Returns 0 on sucess, -1 on error. Complexity is O(1).

clist_insert_before() adds an element before the element pointed by the given iterator in the list. Returns 0 on sucess, -1 on error. Complexity is O(1).

clist_insert_after() adds an element after the element pointed by the given iterator in the list. Returns 0 on sucess, -1 on error. Complexity is O(1).

clist_delete() the elements pointed by the given iterator in the list and returns an iterator to the next element of the list. Complexity is O(1).

Example 2-7. deleting elements in a clist

    #include <libetpan/libetpan.h>

    voir print_content(void * content, void * user_data)
    {
      char * str;

      str = content;

      printf("%s\n", str);
    }

    int main(void)
    {
      clist * list;
      clistiter * iter;

      list = build_string_list();
      if (list == NULL)
        goto err;

      iter = = clist_begin(list);
      while (iter != NULL)
        char * str;

        str = clist_content(iter);
        if (strcmp(str, "foo-bar") == 0)
          iter = clist_delete(list, cur);
        else
          iter = clist_next(iter);
      }

      clist_foreach(list, print_content, NULL);
      printf("\n");

      clist_free(list);

      exit(EXIT_SUCCESS);

     free:
      clist_free(list);
     err:
      exit(EXIT_FAILURE);
    }

## clist_foreach

    typedef void (* clist_func)(void *, void *);

    void clist_foreach(clist * lst, clist_func func, void * data);

clist_foreach() apply a fonction to each element of the list. Complexity is O(n).

## clist_concat

    void clist_concat(clist * dest, clist * src);

clist_concat() adds all the elements of src at the end of dest. Elements are added in the same order. src is an empty list when the operation is finished. Complexity is O(1).

Example 2-8. merging two clists

    #include <libetpan/libetpan.h>

    int main(void)
    {
      clist * list;
      clist * list_2;
      clistiter * iter;

      list = build_string_list();
      if (list == NULL)
        goto err;

      list_2 = build_string_list_2();
      if (list == NULL)
        goto free_list;

      clist_concat(list, list_2);
      clist_free(list_2);

      for(iter = clist_begin(list) ; iter != NULL ; iter =
         clist_next(iter)) {
        char * str;

        str = clist_content(iter);
        printf("%s\n", str);
      }

      clist_free(list);

      exit(EXIT_SUCCESS);

     free_list:
      clist_free(list);
     err:
      exit(EXIT_FAILURE);
    }

Prev | Home | Next
Tools and datatypes | Up | Hash table