Description
The GSList structure and its associated functions provide a standard
singly-linked list data structure.
Each element in the list contains a piece of data, together with a pointer
which links to the next element in the list.
Using this pointer it is possible to move through the list in one
direction only (unlike the
Doubly-Linked Lists
which allow movement in both directions).
The data contained in each element can be either integer values, by using one
of the
Type Conversion Macros,
or simply pointers to any type of data.
List elements are allocated in blocks using a GListAllocator, which is
more efficient than allocating elements individually.
Note that most of the GSList 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.
There is no function to create a GSList. NULL is considered to be the empty
list so you simply set a GSList* to NULL.
To add elements, use g_slist_append(), g_slist_prepend(), g_slist_insert()
and g_slist_insert_sorted().
To remove elements, use g_slist_remove().
To find elements in the list use g_slist_last(), g_slist_next(),
g_slist_nth(), g_slist_nth_data(), g_slist_find() and
g_slist_find_custom().
To find the index of an element use g_slist_position() and g_slist_index().
To call a function for each element in the list use g_slist_foreach().
To free the entire list, use g_slist_free().
Details
struct GSList
struct GSList
{
gpointer data;
GSList *next;
}; |
The GSList struct is used for each element in the singly-linked list.
The data field holds the element's data, which can
be a pointer to any kind of data, or any integer value using the
Type Conversion Macros.
The next field contains the link to the next
element in the list.
g_slist_append ()
Adds a new element on to the end of the list.
Note: The return value is the new start of the list, which may have changed, so
make sure you store the new value.
g_slist_prepend ()
Adds a new element on to the start of the list.
Note: The return value is the new start of the list, which may have changed, so
make sure you store the new value.
g_slist_insert ()
Inserts a new element into the list at the given position.
g_slist_insert_sorted ()
Inserts a new element into the list, using the given comparison function
to determine its position.
g_slist_remove ()
Removes an element from a GSList.
If two elements contain the same data, only the first is removed.
If none of the elements contain the data, the GSList is unchanged.
g_slist_remove_link ()
Removes an element from a GSList, without freeing the element.
The removed element's next link is set to NULL, so that it becomes a
self-contained list with one element.
g_slist_free ()
void g_slist_free (GSList *list); |
Frees all of the memory used by a GSList.
The freed elements are added to the GListAllocator free list.
g_slist_length ()
Gets the number of elements in a GSList.
g_slist_copy ()
Copies a GSList.
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.
g_slist_reverse ()
Reverses a GSList.
g_slist_sort ()
Sorts a GSList using the given comparison function.
g_slist_concat ()
Adds the second GSList onto the end of the first GSList.
Note that the elements of the second GSList are not copied.
They are used directly.
g_slist_foreach ()
Calls a function for each element of a GSList.
g_slist_last ()
Gets the last element in a GSList.
g_slist_next()
#define g_slist_next(slist) |
A convenience macro to gets the next element in a GSList.
g_slist_nth ()
Gets the element at the given position in a GSList.
g_slist_nth_data ()
Gets the data of the element at the given position.
g_slist_find ()
Finds the element in a GSList which contains the given data.
g_slist_find_custom ()
Finds an element in a GSList, 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 gconstpointer arguments, the GSList element's data
and the given user data.
g_slist_position ()
Gets the position of the given element in the GSList (starting from 0).
g_slist_index ()
Gets the position of the element containing the given data (starting from 0).
g_slist_push_allocator ()
void g_slist_push_allocator (GAllocator *allocator); |
Sets the allocator to use to allocate GSList elements.
Use g_slist_pop_allocator() to restore the previous allocator.
g_slist_pop_allocator ()
void g_slist_pop_allocator (void); |
Restores the previous GAllocator, used when allocating GSList elements.