<refentry id="glib-Doubly-Linked-Lists">
<refmeta>
<refentrytitle>Doubly-Linked Lists</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Doubly-Linked Lists</refname><refpurpose>linked lists containing integer values or pointers to data, with the ability
to iterate over the list in both directions.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;


struct      <link linkend="GList">GList</link>;

<link linkend="GList">GList</link>*      <link linkend="g-list-append">g_list_append</link>                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data);
<link linkend="GList">GList</link>*      <link linkend="g-list-prepend">g_list_prepend</link>                  (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data);
<link linkend="GList">GList</link>*      <link linkend="g-list-insert">g_list_insert</link>                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="gint">gint</link> position);
<link linkend="GList">GList</link>*      <link linkend="g-list-insert-before">g_list_insert_before</link>            (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *sibling,
                                             <link linkend="gpointer">gpointer</link> data);
<link linkend="GList">GList</link>*      <link linkend="g-list-insert-sorted">g_list_insert_sorted</link>            (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="GCompareFunc">GCompareFunc</link> func);
<link linkend="GList">GList</link>*      <link linkend="g-list-remove">g_list_remove</link>                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);
<link linkend="GList">GList</link>*      <link linkend="g-list-remove-link">g_list_remove_link</link>              (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *llink);
<link linkend="GList">GList</link>*      <link linkend="g-list-delete-link">g_list_delete_link</link>              (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *link_);
<link linkend="GList">GList</link>*      <link linkend="g-list-remove-all">g_list_remove_all</link>               (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);
<link linkend="void">void</link>        <link linkend="g-list-free">g_list_free</link>                     (<link linkend="GList">GList</link> *list);

<link linkend="GList">GList</link>*      <link linkend="g-list-alloc">g_list_alloc</link>                    (void);
<link linkend="void">void</link>        <link linkend="g-list-free-1">g_list_free_1</link>                   (<link linkend="GList">GList</link> *list);

<link linkend="guint">guint</link>       <link linkend="g-list-length">g_list_length</link>                   (<link linkend="GList">GList</link> *list);
<link linkend="GList">GList</link>*      <link linkend="g-list-copy">g_list_copy</link>                     (<link linkend="GList">GList</link> *list);
<link linkend="GList">GList</link>*      <link linkend="g-list-reverse">g_list_reverse</link>                  (<link linkend="GList">GList</link> *list);
<link linkend="GList">GList</link>*      <link linkend="g-list-sort">g_list_sort</link>                     (<link linkend="GList">GList</link> *list,
                                             <link linkend="GCompareFunc">GCompareFunc</link> compare_func);
<link linkend="gint">gint</link>        (<link linkend="GCompareFunc">*GCompareFunc</link>)                 (<link linkend="gconstpointer">gconstpointer</link> a,
                                             <link linkend="gconstpointer">gconstpointer</link> b);
<link linkend="GList">GList</link>*      <link linkend="g-list-sort-with-data">g_list_sort_with_data</link>           (<link linkend="GList">GList</link> *list,
                                             <link linkend="GCompareDataFunc">GCompareDataFunc</link> compare_func,
                                             <link linkend="gpointer">gpointer</link> user_data);
<link linkend="gint">gint</link>        (<link linkend="GCompareDataFunc">*GCompareDataFunc</link>)             (<link linkend="gconstpointer">gconstpointer</link> a,
                                             <link linkend="gconstpointer">gconstpointer</link> b,
                                             <link linkend="gpointer">gpointer</link> user_data);
<link linkend="GList">GList</link>*      <link linkend="g-list-concat">g_list_concat</link>                   (<link linkend="GList">GList</link> *list1,
                                             <link linkend="GList">GList</link> *list2);
<link linkend="void">void</link>        <link linkend="g-list-foreach">g_list_foreach</link>                  (<link linkend="GList">GList</link> *list,
                                             <link linkend="GFunc">GFunc</link> func,
                                             <link linkend="gpointer">gpointer</link> user_data);
<link linkend="void">void</link>        (<link linkend="GFunc">*GFunc</link>)                        (<link linkend="gpointer">gpointer</link> data,
                                             <link linkend="gpointer">gpointer</link> user_data);

<link linkend="GList">GList</link>*      <link linkend="g-list-first">g_list_first</link>                    (<link linkend="GList">GList</link> *list);
<link linkend="GList">GList</link>*      <link linkend="g-list-last">g_list_last</link>                     (<link linkend="GList">GList</link> *list);
#define     <link linkend="g-list-previous">g_list_previous</link>                 (list)
#define     <link linkend="g-list-next">g_list_next</link>                     (list)
<link linkend="GList">GList</link>*      <link linkend="g-list-nth">g_list_nth</link>                      (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);
<link linkend="gpointer">gpointer</link>    <link linkend="g-list-nth-data">g_list_nth_data</link>                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);
<link linkend="GList">GList</link>*      <link linkend="g-list-nth-prev">g_list_nth_prev</link>                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);

<link linkend="GList">GList</link>*      <link linkend="g-list-find">g_list_find</link>                     (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);
<link linkend="GList">GList</link>*      <link linkend="g-list-find-custom">g_list_find_custom</link>              (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data,
                                             <link linkend="GCompareFunc">GCompareFunc</link> func);
<link linkend="gint">gint</link>        <link linkend="g-list-position">g_list_position</link>                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *llink);
<link linkend="gint">gint</link>        <link linkend="g-list-index">g_list_index</link>                    (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);

<link linkend="void">void</link>        <link linkend="g-list-push-allocator">g_list_push_allocator</link>           (<link linkend="GAllocator">GAllocator</link> *allocator);
<link linkend="void">void</link>        <link linkend="g-list-pop-allocator">g_list_pop_allocator</link>            (void);
</synopsis>
</refsynopsisdiv>









<refsect1>
<title>Description</title>
<para>
The <link linkend="GList"><type>GList</type></link> structure and its associated functions provide a standard
doubly-linked list data structure.
</para>
<para>
Each element in the list contains a piece of data, together with pointers
which link to the previous and next elements in the list.
Using these pointers it is possible to move through the list in both
directions (unlike the
<link linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link>
which only allows movement through the list in the forward direction).
</para>
<para>
The data contained in each element can be either integer values, by using one
of the
<link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
or simply pointers to any type of data.
</para>
<para>
List elements are allocated in blocks using a <link linkend="GAllocator"><type>GAllocator</type></link>, which is
more efficient than allocating elements individually.
</para>
<para>
Note that most of the <link linkend="GList"><type>GList</type></link> functions expect to be passed a pointer to
the first element in the list. The functions which insert elements return
the new start of the list, which may have changed.
</para>
<para>
There is no function to create a <link linkend="GList"><type>GList</type></link>. <literal>NULL</literal> is considered to be the empty
list so you simply set a <link linkend="GList"><type>GList</type></link>* to <literal>NULL</literal>.
</para>
<para>
To add elements, use <link linkend="g-list-append"><function>g_list_append()</function></link>, <link linkend="g-list-prepend"><function>g_list_prepend()</function></link>, <link linkend="g-list-insert"><function>g_list_insert()</function></link>
and <link linkend="g-list-insert-sorted"><function>g_list_insert_sorted()</function></link>.
</para>
<para>
To remove elements, use <link linkend="g-list-remove"><function>g_list_remove()</function></link>.
</para>
<para>
To find elements in the list use <link linkend="g-list-first"><function>g_list_first()</function></link>, <link linkend="g-list-last"><function>g_list_last()</function></link>, <link linkend="g-list-next"><function>g_list_next()</function></link>,
<link linkend="g-list-previous"><function>g_list_previous()</function></link>, <link linkend="g-list-nth"><function>g_list_nth()</function></link>, <link linkend="g-list-nth-data"><function>g_list_nth_data()</function></link>, <link linkend="g-list-find"><function>g_list_find()</function></link> and
<link linkend="g-list-find-custom"><function>g_list_find_custom()</function></link>.
</para>
<para>
To find the index of an element use <link linkend="g-list-position"><function>g_list_position()</function></link> and <link linkend="g-list-index"><function>g_list_index()</function></link>.
</para>
<para>
To call a function for each element in the list use <link linkend="g-list-foreach"><function>g_list_foreach()</function></link>.
</para>
<para>
To free the entire list, use <link linkend="g-list-free"><function>g_list_free()</function></link>.
</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GList"/>struct GList</title>
<indexterm><primary>GList</primary></indexterm><programlisting>struct GList {

  gpointer data;
  GList *next;
  GList *prev;
};
</programlisting>
<para>
The <link linkend="GList"><type>GList</type></link> struct is used for each element in a doubly-linked list.
The <structfield>data</structfield> field holds the element's data, which can
be a pointer to any kind of data, or any integer value using the
<link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>.
The <structfield>next</structfield> and <structfield>prev</structfield>
pointers are the links to the next and previous elements in the list.
</para></refsect2>
<refsect2>
<title><anchor id="g-list-append"/>g_list_append ()</title>
<indexterm><primary>g_list_append</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_append                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
Adds a new element on to the end of the list.
</para>
<note>
<para>
The return value is the new start of the list, which may have changed, so
make sure you store the new value.
</para>
</note>
<informalexample><programlisting>
  /* Notice that these are initialized to the empty list. */
  GList *list = NULL, *number_list = NULL;

  /* This is a list of strings. */
  list = g_list_append (list, "first");
  list = g_list_append (list, "second");

  /* This is a list of integers. */
  number_list = g_list_append (number_list, GINT_TO_POINTER (27));
  number_list = g_list_append (number_list, GINT_TO_POINTER (14));
</programlisting></informalexample><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data for the new element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-prepend"/>g_list_prepend ()</title>
<indexterm><primary>g_list_prepend</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_prepend                  (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
Adds a new element on to the start of the list.
</para>
<note>
<para>
The return value is the new start of the list, which may have changed, so
make sure you store the new value.
</para>
</note>
<informalexample><programlisting>
  /* Notice that it is initialized to the empty list. */
  GList *list = NULL;
  list = g_list_prepend (list, "last");
  list = g_list_prepend (list, "first");
</programlisting></informalexample><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data for the new element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-insert"/>g_list_insert ()</title>
<indexterm><primary>g_list_insert</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_insert                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="gint">gint</link> position);</programlisting>
<para>
Inserts a new element into the list at the given position.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data for the new element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>position</parameter>&nbsp;:</term>
<listitem><simpara>the position to insert the element. If this is negative, or is
larger than the number of elements in the list, the new element is added on
to the end of the list.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-insert-before"/>g_list_insert_before ()</title>
<indexterm><primary>g_list_insert_before</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_insert_before            (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *sibling,
                                             <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
Inserts a new element into the list before the given position.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>sibling</parameter>&nbsp;:</term>
<listitem><simpara>the list element before which the new element is inserted
  or <literal>NULL</literal> to insert at the end of the list.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data for the new element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-insert-sorted"/>g_list_insert_sorted ()</title>
<indexterm><primary>g_list_insert_sorted</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_insert_sorted            (<link linkend="GList">GList</link> *list,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="GCompareFunc">GCompareFunc</link> func);</programlisting>
<para>
Inserts a new element into the list, using the given comparison function
to determine its position.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data for the new element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>func</parameter>&nbsp;:</term>
<listitem><simpara>the function to compare elements in the list. It should return a
number > 0 if the first parameter comes after the second parameter in
the sort order.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-remove"/>g_list_remove ()</title>
<indexterm><primary>g_list_remove</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_remove                   (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);</programlisting>
<para>
Removes an element from a <link linkend="GList"><type>GList</type></link>.
If two elements contain the same data, only the first is removed.
If none of the elements contain the data, the <link linkend="GList"><type>GList</type></link> is unchanged.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data of the element to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-remove-link"/>g_list_remove_link ()</title>
<indexterm><primary>g_list_remove_link</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_remove_link              (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *llink);</programlisting>
<para>
Removes an element from a <link linkend="GList"><type>GList</type></link>, without freeing the element.
The removed element's prev and next links are set to <literal>NULL</literal>, so that it becomes a
self-contained list with one element.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>llink</parameter>&nbsp;:</term>
<listitem><simpara>an element in the <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new start of the <link linkend="GList"><type>GList</type></link>, without the element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-delete-link"/>g_list_delete_link ()</title>
<indexterm><primary>g_list_delete_link</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_delete_link              (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *link_);</programlisting>
<para>
Deletes the node <parameter>link_</parameter> from <parameter>list</parameter>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>link_</parameter>&nbsp;:</term>
<listitem><simpara>node to delete from <parameter>list</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new head of <parameter>list</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-remove-all"/>g_list_remove_all ()</title>
<indexterm><primary>g_list_remove_all</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_remove_all               (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);</programlisting>
<para>
Removes all list nodes with data equal to <parameter>data</parameter>. Returns the new
head of the list. Contrast with <link linkend="g-list-remove"><function>g_list_remove()</function></link> which removes only 
the first node matching the given data.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>data to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>new head of <parameter>list</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-free"/>g_list_free ()</title>
<indexterm><primary>g_list_free</primary></indexterm><programlisting><link linkend="void">void</link>        g_list_free                     (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Frees all of the memory used by a <link linkend="GList"><type>GList</type></link>.
The freed elements are added to the <link linkend="GAllocator"><type>GAllocator</type></link> free list.
</para>
<note>
<para>
If list elements contain dynamically-allocated memory, they should be freed
first.
</para>
</note><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-alloc"/>g_list_alloc ()</title>
<indexterm><primary>g_list_alloc</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_alloc                    (void);</programlisting>
<para>
Allocates space for one <link linkend="GList"><type>GList</type></link> element.
It is called by <link linkend="g-list-append"><function>g_list_append()</function></link>, <link linkend="g-list-prepend"><function>g_list_prepend()</function></link>, <link linkend="g-list-insert"><function>g_list_insert()</function></link> and
<link linkend="g-list-insert-sorted"><function>g_list_insert_sorted()</function></link> and so is rarely used on its own.
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>a pointer to the newly-allocated <link linkend="GList"><type>GList</type></link> element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-free-1"/>g_list_free_1 ()</title>
<indexterm><primary>g_list_free_1</primary></indexterm><programlisting><link linkend="void">void</link>        g_list_free_1                   (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Frees one <link linkend="GList"><type>GList</type></link> element.
It is usually used after <link linkend="g-list-remove-link"><function>g_list_remove_link()</function></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link> element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-length"/>g_list_length ()</title>
<indexterm><primary>g_list_length</primary></indexterm><programlisting><link linkend="guint">guint</link>       g_list_length                   (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Gets the number of elements in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the number of elements in the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-copy"/>g_list_copy ()</title>
<indexterm><primary>g_list_copy</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_copy                     (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Copies a <link linkend="GList"><type>GList</type></link>.
</para>
<para>
Note that this is a "shallow" copy. If the list elements consist of pointers
to data, the pointers are copied but the actual data isn't.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>a copy of <parameter>list</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-reverse"/>g_list_reverse ()</title>
<indexterm><primary>g_list_reverse</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_reverse                  (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Reverses a <link linkend="GList"><type>GList</type></link>.
It simply switches the next and prev pointers of each element.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the start of the reversed <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-sort"/>g_list_sort ()</title>
<indexterm><primary>g_list_sort</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_sort                     (<link linkend="GList">GList</link> *list,
                                             <link linkend="GCompareFunc">GCompareFunc</link> compare_func);</programlisting>
<para>
Sorts a <link linkend="GList"><type>GList</type></link> using the given comparison function.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>compare_func</parameter>&nbsp;:</term>
<listitem><simpara>the comparison function used to sort the <link linkend="GList"><type>GList</type></link>. This function
is passed 2 elements of the <link linkend="GList"><type>GList</type></link> and should return 0 if they are equal,
a negative value if the first element comes before the second, or a positive
value if the first element comes after the second.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the start of the sorted <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="GCompareFunc"/>GCompareFunc ()</title>
<indexterm><primary>GCompareFunc</primary></indexterm><programlisting><link linkend="gint">gint</link>        (*GCompareFunc)                 (<link linkend="gconstpointer">gconstpointer</link> a,
                                             <link linkend="gconstpointer">gconstpointer</link> b);</programlisting>
<para>
Specifies the type of a comparison function used to compare two
values.  The function should return a negative integer if the first
value comes before the second, 0 if they are equal, or a positive
integer if the first value comes after the second.
</para><variablelist role="params">
<varlistentry><term><parameter>a</parameter>&nbsp;:</term>
<listitem><simpara>a value.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>b</parameter>&nbsp;:</term>
<listitem><simpara>a value to compare with.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>negative value if <parameter>a</parameter> &lt; <parameter>b</parameter>; zero if <parameter>a</parameter> = <parameter>b</parameter>; positive value
if <parameter>a</parameter> > <parameter>b</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-sort-with-data"/>g_list_sort_with_data ()</title>
<indexterm><primary>g_list_sort_with_data</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_sort_with_data           (<link linkend="GList">GList</link> *list,
                                             <link linkend="GCompareDataFunc">GCompareDataFunc</link> compare_func,
                                             <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Like <link linkend="g-list-sort"><function>g_list_sort()</function></link>, but the comparison function accepts a user data argument.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>compare_func</parameter>&nbsp;:</term>
<listitem><simpara>comparison function.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data to pass to comparison function.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the new head of <parameter>list</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="GCompareDataFunc"/>GCompareDataFunc ()</title>
<indexterm><primary>GCompareDataFunc</primary></indexterm><programlisting><link linkend="gint">gint</link>        (*GCompareDataFunc)             (<link linkend="gconstpointer">gconstpointer</link> a,
                                             <link linkend="gconstpointer">gconstpointer</link> b,
                                             <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Specifies the type of a comparison function used to compare two
values.  The function should return a negative integer if the first
value comes before the second, 0 if they are equal, or a positive
integer if the first value comes after the second.
</para><variablelist role="params">
<varlistentry><term><parameter>a</parameter>&nbsp;:</term>
<listitem><simpara>a value.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>b</parameter>&nbsp;:</term>
<listitem><simpara>a value to compare with.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data to pass to comparison function.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>negative value if <parameter>a</parameter> &lt; <parameter>b</parameter>; zero if <parameter>a</parameter> = <parameter>b</parameter>; positive value
if <parameter>a</parameter> > <parameter>b</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-concat"/>g_list_concat ()</title>
<indexterm><primary>g_list_concat</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_concat                   (<link linkend="GList">GList</link> *list1,
                                             <link linkend="GList">GList</link> *list2);</programlisting>
<para>
Adds the second <link linkend="GList"><type>GList</type></link> onto the end of the first <link linkend="GList"><type>GList</type></link>.
Note that the elements of the second <link linkend="GList"><type>GList</type></link> are not copied.
They are used directly.
</para><variablelist role="params">
<varlistentry><term><parameter>list1</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>list2</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GList"><type>GList</type></link> to add to the end of the first <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the start of the new <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-foreach"/>g_list_foreach ()</title>
<indexterm><primary>g_list_foreach</primary></indexterm><programlisting><link linkend="void">void</link>        g_list_foreach                  (<link linkend="GList">GList</link> *list,
                                             <link linkend="GFunc">GFunc</link> func,
                                             <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Calls a function for each element of a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>func</parameter>&nbsp;:</term>
<listitem><simpara>the function to call with each element's data.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data to pass to the function.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="GFunc"/>GFunc ()</title>
<indexterm><primary>GFunc</primary></indexterm><programlisting><link linkend="void">void</link>        (*GFunc)                        (<link linkend="gpointer">gpointer</link> data,
                                             <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Specifies the type of functions passed to <link linkend="g-list-foreach"><function>g_list_foreach()</function></link> and
<link linkend="g-slist-foreach"><function>g_slist_foreach()</function></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the element's data.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data passed to <link linkend="g-list-foreach"><function>g_list_foreach()</function></link> or <link linkend="g-slist-foreach"><function>g_slist_foreach()</function></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-first"/>g_list_first ()</title>
<indexterm><primary>g_list_first</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_first                    (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Gets the first element in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the first element in a <link linkend="GList"><type>GList</type></link>, or <literal>NULL</literal> if the <link linkend="GList"><type>GList</type></link> has no elements.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-last"/>g_list_last ()</title>
<indexterm><primary>g_list_last</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_last                     (<link linkend="GList">GList</link> *list);</programlisting>
<para>
Gets the last element in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the last element in the <link linkend="GList"><type>GList</type></link>, or <literal>NULL</literal> if the <link linkend="GList"><type>GList</type></link> has no
elements.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-previous"/>g_list_previous()</title>
<indexterm><primary>g_list_previous</primary></indexterm><programlisting>#define     g_list_previous(list)</programlisting>
<para>
A convenience macro to gets the previous element in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>an element in a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the previous element, or <literal>NULL</literal> if there are no previous elements.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-next"/>g_list_next()</title>
<indexterm><primary>g_list_next</primary></indexterm><programlisting>#define     g_list_next(list)</programlisting>
<para>
A convenience macro to gets the next element in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>an element in a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the next element, or <literal>NULL</literal> if there are no more elements.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-nth"/>g_list_nth ()</title>
<indexterm><primary>g_list_nth</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_nth                      (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);</programlisting>
<para>
Gets the element at the given position in a <link linkend="GList"><type>GList</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>n</parameter>&nbsp;:</term>
<listitem><simpara>the position of the element, counting from 0.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the element, or <literal>NULL</literal> if the position is off the end of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-nth-data"/>g_list_nth_data ()</title>
<indexterm><primary>g_list_nth_data</primary></indexterm><programlisting><link linkend="gpointer">gpointer</link>    g_list_nth_data                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);</programlisting>
<para>
Gets the data of the element at the given position.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>n</parameter>&nbsp;:</term>
<listitem><simpara>the position of the element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the element's data, or <literal>NULL</literal> if the position is off the end of the
<link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-nth-prev"/>g_list_nth_prev ()</title>
<indexterm><primary>g_list_nth_prev</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_nth_prev                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="guint">guint</link> n);</programlisting>
<para>
Gets the element <parameter>n</parameter> places before <parameter>list</parameter>.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>n</parameter>&nbsp;:</term>
<listitem><simpara>the position of the element, counting from 0.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the element, or <literal>NULL</literal> if the position is off the end of the <link linkend="GList"><type>GList</type></link>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-find"/>g_list_find ()</title>
<indexterm><primary>g_list_find</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_find                     (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);</programlisting>
<para>
Finds the element in a <link linkend="GList"><type>GList</type></link> which contains the given data.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the element data to find.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the found <link linkend="GList"><type>GList</type></link> element, or <literal>NULL</literal> if it is not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-find-custom"/>g_list_find_custom ()</title>
<indexterm><primary>g_list_find_custom</primary></indexterm><programlisting><link linkend="GList">GList</link>*      g_list_find_custom              (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data,
                                             <link linkend="GCompareFunc">GCompareFunc</link> func);</programlisting>
<para>
Finds an element in a <link linkend="GList"><type>GList</type></link>, using a supplied function to find the desired
element.
It iterates over the list, calling the given function which should return 0
when the desired element is found.
The function takes two <link linkend="gconstpointer"><type>gconstpointer</type></link> arguments, the <link linkend="GList"><type>GList</type></link> element's data
and the given user data.
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>user data passed to the function.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>func</parameter>&nbsp;:</term>
<listitem><simpara>the function to call for each element. It should return 0 when the
desired element is found.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the found <link linkend="GList"><type>GList</type></link> element, or <literal>NULL</literal> if it is not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-position"/>g_list_position ()</title>
<indexterm><primary>g_list_position</primary></indexterm><programlisting><link linkend="gint">gint</link>        g_list_position                 (<link linkend="GList">GList</link> *list,
                                             <link linkend="GList">GList</link> *llink);</programlisting>
<para>
Gets the position of the given element in the <link linkend="GList"><type>GList</type></link> (starting from 0).
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>llink</parameter>&nbsp;:</term>
<listitem><simpara>an element in the <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the position of the element in the <link linkend="GList"><type>GList</type></link>, or -1 if the element is
not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-index"/>g_list_index ()</title>
<indexterm><primary>g_list_index</primary></indexterm><programlisting><link linkend="gint">gint</link>        g_list_index                    (<link linkend="GList">GList</link> *list,
                                             <link linkend="gconstpointer">gconstpointer</link> data);</programlisting>
<para>
Gets the position of the element containing the given data (starting from 0).
</para><variablelist role="params">
<varlistentry><term><parameter>list</parameter>&nbsp;:</term>
<listitem><simpara>a <link linkend="GList"><type>GList</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data to find.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the index of the element containing the data, or -1 if the data
is not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-push-allocator"/>g_list_push_allocator ()</title>
<indexterm><primary>g_list_push_allocator</primary></indexterm><programlisting><link linkend="void">void</link>        g_list_push_allocator           (<link linkend="GAllocator">GAllocator</link> *allocator);</programlisting>
<para>
Sets the allocator to use to allocate <link linkend="GList"><type>GList</type></link> elements.
Use <link linkend="g-list-pop-allocator"><function>g_list_pop_allocator()</function></link> to restore the previous allocator.
</para><variablelist role="params">
<varlistentry><term><parameter>allocator</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GAllocator"><type>GAllocator</type></link> to use when allocating <link linkend="GList"><type>GList</type></link> elements.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-list-pop-allocator"/>g_list_pop_allocator ()</title>
<indexterm><primary>g_list_pop_allocator</primary></indexterm><programlisting><link linkend="void">void</link>        g_list_pop_allocator            (void);</programlisting>
<para>
Restores the previous <link linkend="GAllocator"><type>GAllocator</type></link>, used when allocating <link linkend="GList"><type>GList</type></link> elements.
</para></refsect2>

</refsect1>




</refentry>
