Top |
Functions
GSList * | g_slist_alloc () |
GSList * | g_slist_append () |
GSList * | g_slist_prepend () |
GSList * | g_slist_insert () |
GSList * | g_slist_insert_before () |
GSList * | g_slist_insert_sorted () |
GSList * | g_slist_remove () |
GSList * | g_slist_remove_link () |
GSList * | g_slist_delete_link () |
GSList * | g_slist_remove_all () |
void | g_slist_free () |
void | g_slist_free_full () |
void | g_slist_free_1 () |
guint | g_slist_length () |
GSList * | g_slist_copy () |
GSList * | g_slist_copy_deep () |
GSList * | g_slist_reverse () |
GSList * | g_slist_insert_sorted_with_data () |
GSList * | g_slist_sort () |
GSList * | g_slist_sort_with_data () |
GSList * | g_slist_concat () |
void | g_slist_foreach () |
GSList * | g_slist_last () |
#define | g_slist_next() |
GSList * | g_slist_nth () |
gpointer | g_slist_nth_data () |
GSList * | g_slist_find () |
GSList * | g_slist_find_custom () |
gint | g_slist_position () |
gint | g_slist_index () |
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 double-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 from the slice allocator, 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()
.
Functions
g_slist_alloc ()
GSList *
g_slist_alloc (void
);
Allocates space for one GSList element. It is called by the
g_slist_append()
, g_slist_prepend()
, g_slist_insert()
and
g_slist_insert_sorted()
functions and so is rarely used on its own.
g_slist_append ()
GSList * g_slist_append (GSList *list
,gpointer data
);
Adds a new element on to the end of the list.
The return value is the new start of the list, which may have changed, so make sure you store the new value.
Note that g_slist_append()
has to traverse the entire list
to find the end, which is inefficient when adding multiple
elements. A common idiom to avoid the inefficiency is to prepend
the elements and reverse the list when all elements have been added.
1 2 3 4 5 6 7 8 9 10 |
// Notice that these are initialized to the empty list. GSList *list = NULL, *number_list = NULL; // This is a list of strings. list = g_slist_append (list, "first"); list = g_slist_append (list, "second"); // This is a list of integers. number_list = g_slist_append (number_list, GINT_TO_POINTER (27)); number_list = g_slist_append (number_list, GINT_TO_POINTER (14)); |
g_slist_prepend ()
GSList * g_slist_prepend (GSList *list
,gpointer data
);
Adds a new element on to the start of the list.
The return value is the new start of the list, which may have changed, so make sure you store the new value.
1 2 3 4 |
// Notice that it is initialized to the empty list. GSList *list = NULL; list = g_slist_prepend (list, "last"); list = g_slist_prepend (list, "first"); |
g_slist_insert ()
GSList * g_slist_insert (GSList *list
,gpointer data
,gint position
);
Inserts a new element into the list at the given position.
Parameters
list |
a GSList |
|
data |
the data for the new element |
|
position |
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. |
g_slist_insert_before ()
GSList * g_slist_insert_before (GSList *slist
,GSList *sibling
,gpointer data
);
Inserts a node before sibling
containing data
.
Parameters
slist |
a GSList |
|
sibling |
node to insert |
|
data |
data to put in the newly-inserted node |
g_slist_insert_sorted ()
GSList * g_slist_insert_sorted (GSList *list
,gpointer data
,GCompareFunc func
);
Inserts a new element into the list, using the given comparison function to determine its position.
Parameters
list |
a GSList |
|
data |
the data for the new element |
|
func |
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. |
g_slist_remove ()
GSList * g_slist_remove (GSList *list
,gconstpointer data
);
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 ()
GSList * g_slist_remove_link (GSList *list
,GSList *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.
Removing arbitrary nodes from a singly-linked list
requires time that is proportional to the length of the list
(ie. O(n)). If you find yourself using g_slist_remove_link()
frequently, you should consider a different data structure,
such as the doubly-linked GList.
g_slist_delete_link ()
GSList * g_slist_delete_link (GSList *list
,GSList *link_
);
Removes the node link_ from the list and frees it.
Compare this to g_slist_remove_link()
which removes the node
without freeing it.
Removing arbitrary nodes from a singly-linked list requires time
that is proportional to the length of the list (ie. O(n)). If you
find yourself using g_slist_delete_link()
frequently, you should
consider a different data structure, such as the doubly-linked
GList.
g_slist_remove_all ()
GSList * g_slist_remove_all (GSList *list
,gconstpointer data
);
Removes all list nodes with data equal to data
.
Returns the new head of the list. Contrast with
g_slist_remove()
which removes only the first node
matching the given data.
g_slist_free ()
void
g_slist_free (GSList *list
);
Frees all of the memory used by a GSList. The freed elements are returned to the slice allocator.
If list elements contain dynamically-allocated memory,
you should either use g_slist_free_full()
or free them manually
first.
g_slist_free_full ()
void g_slist_free_full (GSList *list
,GDestroyNotify free_func
);
Convenience method, which frees all the memory used by a GSList, and calls the specified destroy function on every element's data.
free_func
must not modify the list (eg, by removing the freed
element from it).
Parameters
list |
a pointer to a GSList |
|
free_func |
the function to be called to free each element's data |
Since: 2.28
g_slist_free_1 ()
void
g_slist_free_1 (GSList *list
);
Frees one GSList element.
It is usually used after g_slist_remove_link()
.
g_slist_length ()
guint
g_slist_length (GSList *list
);
Gets the number of elements in a GSList.
This function iterates over the whole list to
count its elements. To check whether the list is non-empty, it is faster to
check list
against NULL
.
g_slist_copy ()
GSList *
g_slist_copy (GSList *list
);
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. See g_slist_copy_deep()
if you need
to copy the data as well.
g_slist_copy_deep ()
GSList * g_slist_copy_deep (GSList *list
,GCopyFunc func
,gpointer user_data
);
Makes a full (deep) copy of a GSList.
In contrast with g_slist_copy()
, this function uses func
to make a copy of
each list element, in addition to copying the list container itself.
func
, as a GCopyFunc, takes two arguments, the data to be copied and a user
pointer. It's safe to pass NULL as user_data, if the copy function takes only
one argument.
For instance, if list
holds a list of GObjects, you can do:
1 |
another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL); |
And, to entirely free the new list, you could do:
1 |
g_slist_free_full (another_list, g_object_unref); |
Since: 2.34
g_slist_insert_sorted_with_data ()
GSList * g_slist_insert_sorted_with_data (GSList *list
,gpointer data
,GCompareDataFunc func
,gpointer user_data
);
Inserts a new element into the list, using the given comparison function to determine its position.
Parameters
list |
a GSList |
|
data |
the data for the new element |
|
func |
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. |
|
user_data |
data to pass to comparison function |
Since: 2.10
g_slist_sort ()
GSList * g_slist_sort (GSList *list
,GCompareFunc compare_func
);
Sorts a GSList using the given comparison function. The algorithm used is a stable sort.
Parameters
list |
a GSList |
|
compare_func |
the comparison function used to sort the GSList. This function is passed the data from 2 elements of the GSList 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. |
g_slist_sort_with_data ()
GSList * g_slist_sort_with_data (GSList *list
,GCompareDataFunc compare_func
,gpointer user_data
);
Like g_slist_sort()
, but the sort function accepts a user data argument.
Parameters
list |
a GSList |
|
compare_func |
comparison function |
|
user_data |
data to pass to comparison function |
g_slist_concat ()
GSList * g_slist_concat (GSList *list1
,GSList *list2
);
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 ()
void g_slist_foreach (GSList *list
,GFunc func
,gpointer user_data
);
Calls a function for each element of a GSList.
It is safe for func
to remove the element from list
, but it must
not modify any part of the list after that element.
Parameters
list |
a GSList |
|
func |
the function to call with each element's data |
|
user_data |
user data to pass to the function |
g_slist_last ()
GSList *
g_slist_last (GSList *list
);
Gets the last element in a GSList.
This function iterates over the whole list.
g_slist_next()
#define g_slist_next(slist)
A convenience macro to get the next element in a GSList.
Note that it is considered perfectly acceptable to access
slist->next
directly.
g_slist_nth ()
GSList * g_slist_nth (GSList *list
,guint n
);
Gets the element at the given position in a GSList.
g_slist_nth_data ()
gpointer g_slist_nth_data (GSList *list
,guint n
);
Gets the data of the element at the given position.
g_slist_find ()
GSList * g_slist_find (GSList *list
,gconstpointer data
);
Finds the element in a GSList which contains the given data.
g_slist_find_custom ()
GSList * g_slist_find_custom (GSList *list
,gconstpointer data
,GCompareFunc func
);
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 as the first argument and the given user data.
Parameters
list |
a GSList |
|
data |
user data passed to the function |
|
func |
the function to call for each element. It should return 0 when the desired element is found |
g_slist_position ()
gint g_slist_position (GSList *list
,GSList *llink
);
Gets the position of the given element in the GSList (starting from 0).
g_slist_index ()
gint g_slist_index (GSList *list
,gconstpointer data
);
Gets the position of the element containing the given data (starting from 0).
Types and Values
struct GSList
struct GSList { gpointer data; GSList *next; };
The GSList struct is used for each element in the singly-linked list.
Members
gpointer |
holds the element's data, which can be a pointer to any kind of data, or any integer value using the Type Conversion Macros |
|
GSList * |
contains the link to the next element in the list. |