📄 linked_lists_single.sgml
字号:
<refentry id="glib-Singly-Linked-Lists"><refmeta><refentrytitle>Singly-Linked Lists</refentrytitle><manvolnum>3</manvolnum><refmiscinfo>GLIB Library</refmiscinfo></refmeta><refnamediv><refname>Singly-Linked Lists</refname><refpurpose>linked lists containing integer values or pointers to data, limited toiterating over the list in one direction.</refpurpose></refnamediv><refsynopsisdiv><title>Synopsis</title><synopsis>#include <glib.h>struct <link linkend="GSList">GSList</link>;<link linkend="GSList">GSList</link>* <link linkend="g-slist-alloc">g_slist_alloc</link> (void);<link linkend="GSList">GSList</link>* <link linkend="g-slist-append">g_slist_append</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gpointer">gpointer</link> data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-prepend">g_slist_prepend</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gpointer">gpointer</link> data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-insert">g_slist_insert</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gpointer">gpointer</link> data, <link linkend="gint">gint</link> position);<link linkend="GSList">GSList</link>* <link linkend="g-slist-insert-before">g_slist_insert_before</link> (<link linkend="GSList">GSList</link> *slist, <link linkend="GSList">GSList</link> *sibling, <link linkend="gpointer">gpointer</link> data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-insert-sorted">g_slist_insert_sorted</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gpointer">gpointer</link> data, <link linkend="GCompareFunc">GCompareFunc</link> func);<link linkend="GSList">GSList</link>* <link linkend="g-slist-remove">g_slist_remove</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gconstpointer">gconstpointer</link> data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-remove-link">g_slist_remove_link</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GSList">GSList</link> *link);<link linkend="GSList">GSList</link>* <link linkend="g-slist-delete-link">g_slist_delete_link</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GSList">GSList</link> *link);<link linkend="GSList">GSList</link>* <link linkend="g-slist-remove-all">g_slist_remove_all</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gconstpointer">gconstpointer</link> data);void <link linkend="g-slist-free">g_slist_free</link> (<link linkend="GSList">GSList</link> *list);void <link linkend="g-slist-free-1">g_slist_free_1</link> (<link linkend="GSList">GSList</link> *list);<link linkend="guint">guint</link> <link linkend="g-slist-length">g_slist_length</link> (<link linkend="GSList">GSList</link> *list);<link linkend="GSList">GSList</link>* <link linkend="g-slist-copy">g_slist_copy</link> (<link linkend="GSList">GSList</link> *list);<link linkend="GSList">GSList</link>* <link linkend="g-slist-reverse">g_slist_reverse</link> (<link linkend="GSList">GSList</link> *list);<link linkend="GSList">GSList</link>* <link linkend="g-slist-sort">g_slist_sort</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GCompareFunc">GCompareFunc</link> compare_func);<link linkend="GSList">GSList</link>* <link linkend="g-slist-sort-with-data">g_slist_sort_with_data</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GCompareDataFunc">GCompareDataFunc</link> compare_func, <link linkend="gpointer">gpointer</link> user_data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-concat">g_slist_concat</link> (<link linkend="GSList">GSList</link> *list1, <link linkend="GSList">GSList</link> *list2);void <link linkend="g-slist-foreach">g_slist_foreach</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GFunc">GFunc</link> func, <link linkend="gpointer">gpointer</link> user_data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-last">g_slist_last</link> (<link linkend="GSList">GSList</link> *list);#define <link linkend="g-slist-next">g_slist_next</link> (slist)<link linkend="GSList">GSList</link>* <link linkend="g-slist-nth">g_slist_nth</link> (<link linkend="GSList">GSList</link> *list, <link linkend="guint">guint</link> n);<link linkend="gpointer">gpointer</link> <link linkend="g-slist-nth-data">g_slist_nth_data</link> (<link linkend="GSList">GSList</link> *list, <link linkend="guint">guint</link> n);<link linkend="GSList">GSList</link>* <link linkend="g-slist-find">g_slist_find</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gconstpointer">gconstpointer</link> data);<link linkend="GSList">GSList</link>* <link linkend="g-slist-find-custom">g_slist_find_custom</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gconstpointer">gconstpointer</link> data, <link linkend="GCompareFunc">GCompareFunc</link> func);<link linkend="gint">gint</link> <link linkend="g-slist-position">g_slist_position</link> (<link linkend="GSList">GSList</link> *list, <link linkend="GSList">GSList</link> *llink);<link linkend="gint">gint</link> <link linkend="g-slist-index">g_slist_index</link> (<link linkend="GSList">GSList</link> *list, <link linkend="gconstpointer">gconstpointer</link> data);void <link linkend="g-slist-push-allocator">g_slist_push_allocator</link> (<link linkend="GAllocator">GAllocator</link> *allocator);void <link linkend="g-slist-pop-allocator">g_slist_pop_allocator</link> (void);</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The <link linkend="GSList">GSList</link> structure and its associated functions provide a standardsingly-linked list data structure.</para><para>Each element in the list contains a piece of data, together with a pointerwhich links to the next element in the list.Using this pointer it is possible to move through the list in onedirection only (unlike the<link linkend="glib-Doubly-Linked-lists">Doubly-Linked Lists</link>which allow movement in both directions).</para><para>The data contained in each element can be either integer values, by using oneof 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="GListAllocator">GListAllocator</link>, which ismore efficient than allocating elements individually.</para><para>Note that most of the <link linkend="GSList">GSList</link> functions expect to be passed a pointer tothe first element in the list. The functions which insert elements returnthe new start of the list, which may have changed.</para><para>There is no function to create a <link linkend="GSList">GSList</link>. <literal>NULL</literal> is considered to be the emptylist so you simply set a <link linkend="GSList">GSList</link>* to <literal>NULL</literal>.</para><para>To add elements, use <link linkend="g-slist-append">g_slist_append</link>(), <link linkend="g-slist-prepend">g_slist_prepend</link>(), <link linkend="g-slist-insert">g_slist_insert</link>()and <link linkend="g-slist-insert-sorted">g_slist_insert_sorted</link>().</para><para>To remove elements, use <link linkend="g-slist-remove">g_slist_remove</link>().</para><para>To find elements in the list use <link linkend="g-slist-last">g_slist_last</link>(), <link linkend="g-slist-next">g_slist_next</link>(),<link linkend="g-slist-nth">g_slist_nth</link>(), <link linkend="g-slist-nth-data">g_slist_nth_data</link>(), <link linkend="g-slist-find">g_slist_find</link>() and<link linkend="g-slist-find-custom">g_slist_find_custom</link>().</para><para>To find the index of an element use <link linkend="g-slist-position">g_slist_position</link>() and <link linkend="g-slist-index">g_slist_index</link>().</para><para>To call a function for each element in the list use <link linkend="g-slist-foreach">g_slist_foreach</link>().</para><para>To free the entire list, use <link linkend="g-slist-free">g_slist_free</link>().</para></refsect1><refsect1><title>Details</title><refsect2><title><anchor id="GSList">struct GSList</title><programlisting>struct GSList{ gpointer data; GSList *next;};</programlisting><para>The <link linkend="GSList">GSList</link> struct is used for each element in the singly-linked list.The <structfield>data</structfield> field holds the element's data, which canbe 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> field contains the link to the nextelement in the list.</para></refsect2><refsect2><title><anchor id="g-slist-alloc">g_slist_alloc ()</title><programlisting><link linkend="GSList">GSList</link>* g_slist_alloc (void);</programlisting><para>Allocates space for one <link linkend="GSList">GSList</link> element.It is called by the <link linkend="g-slist-append">g_slist_append</link>(), <link linkend="g-slist-prepend">g_slist_prepend</link>(), <link linkend="g-slist-insert">g_slist_insert</link>() and<link linkend="g-slist-insert-sorted">g_slist_insert_sorted</link>() functions and so is rarely used on its own.</para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>a pointer to the newly-allocated <link linkend="GSList">GSList</link> element.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-slist-append">g_slist_append ()</title><programlisting><link linkend="GSList">GSList</link>* g_slist_append (<link linkend="GSList">GSList</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, somake sure you store the new value.</para></note><informalexample><programlisting> /* 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));</programlisting></informalexample><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>list</parameter> :</entry><entry>a <link linkend="GSList">GSList</link>.</entry></row><row><entry align="right"><parameter>data</parameter> :</entry><entry>the data for the new element.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the new start of the <link linkend="GSList">GSList</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-slist-prepend">g_slist_prepend ()</title><programlisting><link linkend="GSList">GSList</link>* g_slist_prepend (<link linkend="GSList">GSList</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, somake sure you store the new value.</para></note><informalexample><programlisting> /* Notice that it is initialized to the empty list. */ GSList *list = NULL; list = g_slist_prepend (list, "last"); list = g_slist_prepend (list, "first");</programlisting></informalexample><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>list</parameter> :</entry><entry>a <link linkend="GSList">GSList</link>.</entry></row><row><entry align="right"><parameter>data</parameter> :</entry><entry>the data for the new element.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the new start of the <link linkend="GSList">GSList</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-slist-insert">g_slist_insert ()</title><programlisting><link linkend="GSList">GSList</link>* g_slist_insert (<link linkend="GSList">GSList</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><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>list</parameter> :</entry><entry>a <link linkend="GSList">GSList</link>.</entry></row><row><entry align="right"><parameter>data</parameter> :</entry><entry>the data for the new element.</entry></row><row><entry align="right"><parameter>position</parameter> :</entry><entry>the position to insert the element. If this is negative, or islarger than the number of elements in the list, the new element is added onto the end of the list.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the new start of the <link linkend="GSList">GSList</link>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -