📄 glib-doubly-linked-lists.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Doubly-Linked Lists</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-data-types.html" title="GLib Data Types"><link rel="prev" href="glib-Memory-Chunks.html" title="Memory Chunks"><link rel="next" href="glib-Singly-Linked-Lists.html" title="Singly-Linked Lists"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Memory-Chunks.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-data-types.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Singly-Linked-Lists.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Doubly-Linked-Lists"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Doubly-Linked Lists</span></h2><p>Doubly-Linked Lists — linked lists containing integer values or pointers to data, with the abilityto iterate over the list in both directions.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Doubly-Linked-Lists.html#GList">GList</a>;<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-append">g_list_append</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-prepend">g_list_prepend</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-insert">g_list_insert</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a href="glib-Basic-Types.html#gint">gint</a> position);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-insert-before">g_list_insert_before</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *sibling, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-insert-sorted">g_list_insert_sorted</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a href="glib-Doubly-Linked-Lists.html#GCompareFunc">GCompareFunc</a> func);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-remove">g_list_remove</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-remove-link">g_list_remove_link</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *llink);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-delete-link">g_list_delete_link</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *link_);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-remove-all">g_list_remove_all</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data);void <a href="glib-Doubly-Linked-Lists.html#g-list-free">g_list_free</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-alloc">g_list_alloc</a> (void);void <a href="glib-Doubly-Linked-Lists.html#g-list-free-1">g_list_free_1</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-Doubly-Linked-Lists.html#g-list-length">g_list_length</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-copy">g_list_copy</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-reverse">g_list_reverse</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-sort">g_list_sort</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GCompareFunc">GCompareFunc</a> compare_func);<a href="glib-Basic-Types.html#gint">gint</a> (<a href="glib-Doubly-Linked-Lists.html#GCompareFunc">*GCompareFunc</a>) (<a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> a, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> b);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-sort-with-data">g_list_sort_with_data</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GCompareDataFunc">GCompareDataFunc</a> compare_func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a href="glib-Basic-Types.html#gint">gint</a> (<a href="glib-Doubly-Linked-Lists.html#GCompareDataFunc">*GCompareDataFunc</a>) (<a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> a, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> b, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-concat">g_list_concat</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list1, <a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list2);void <a href="glib-Doubly-Linked-Lists.html#g-list-foreach">g_list_foreach</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GFunc">GFunc</a> func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);void (<a href="glib-Doubly-Linked-Lists.html#GFunc">*GFunc</a>) (<a href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-first">g_list_first</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-last">g_list_last</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list);#define <a href="glib-Doubly-Linked-Lists.html#g-list-previous">g_list_previous</a> (list)#define <a href="glib-Doubly-Linked-Lists.html#g-list-next">g_list_next</a> (list)<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-nth">g_list_nth</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#guint">guint</a> n);<a href="glib-Basic-Types.html#gpointer">gpointer</a> <a href="glib-Doubly-Linked-Lists.html#g-list-nth-data">g_list_nth_data</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#guint">guint</a> n);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-nth-prev">g_list_nth_prev</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#guint">guint</a> n);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-find">g_list_find</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data);<a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* <a href="glib-Doubly-Linked-Lists.html#g-list-find-custom">g_list_find_custom</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data, <a href="glib-Doubly-Linked-Lists.html#GCompareFunc">GCompareFunc</a> func);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-Doubly-Linked-Lists.html#g-list-position">g_list_position</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *llink);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-Doubly-Linked-Lists.html#g-list-index">g_list_index</a> (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data);void <a href="glib-Doubly-Linked-Lists.html#g-list-push-allocator">g_list_push_allocator</a> (<a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a> *allocator);void <a href="glib-Doubly-Linked-Lists.html#g-list-pop-allocator">g_list_pop_allocator</a> (void);</pre></div><div class="refsect1" lang="en"><a name="id3105868"></a><h2>Description</h2><p>The <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a> structure and its associated functions provide a standarddoubly-linked list data structure.</p><p>Each element in the list contains a piece of data, together with pointerswhich link to the previous and next elements in the list.Using these pointers it is possible to move through the list in bothdirections (unlike theSingly-Linked Listswhich only allows movement through the list in the forward direction).</p><p>The data contained in each element can be either integer values, by using oneof the<a href="glib-Type-Conversion-Macros.html" title="Type Conversion Macros">Type Conversion Macros</a>,or simply pointers to any type of data.</p><p>List elements are allocated in blocks using a <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>, which ismore efficient than allocating elements individually.</p><p>Note that most of the <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a> 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.</p><p>There is no function to create a <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>. <code class="literal">NULL</code> is considered to be the emptylist so you simply set a <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>* to <code class="literal">NULL</code>.</p><p>To add elements, use <a href="glib-Doubly-Linked-Lists.html#g-list-append"><code class="function">g_list_append()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-prepend"><code class="function">g_list_prepend()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-insert"><code class="function">g_list_insert()</code></a>and <a href="glib-Doubly-Linked-Lists.html#g-list-insert-sorted"><code class="function">g_list_insert_sorted()</code></a>.</p><p>To remove elements, use <a href="glib-Doubly-Linked-Lists.html#g-list-remove"><code class="function">g_list_remove()</code></a>.</p><p>To find elements in the list use <a href="glib-Doubly-Linked-Lists.html#g-list-first"><code class="function">g_list_first()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-last"><code class="function">g_list_last()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-next"><code class="function">g_list_next()</code></a>,<a href="glib-Doubly-Linked-Lists.html#g-list-previous"><code class="function">g_list_previous()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-nth"><code class="function">g_list_nth()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-nth-data"><code class="function">g_list_nth_data()</code></a>, <a href="glib-Doubly-Linked-Lists.html#g-list-find"><code class="function">g_list_find()</code></a> and<a href="glib-Doubly-Linked-Lists.html#g-list-find-custom"><code class="function">g_list_find_custom()</code></a>.</p><p>To find the index of an element use <a href="glib-Doubly-Linked-Lists.html#g-list-position"><code class="function">g_list_position()</code></a> and <a href="glib-Doubly-Linked-Lists.html#g-list-index"><code class="function">g_list_index()</code></a>.</p><p>To call a function for each element in the list use <a href="glib-Doubly-Linked-Lists.html#g-list-foreach"><code class="function">g_list_foreach()</code></a>.</p><p>To free the entire list, use <a href="glib-Doubly-Linked-Lists.html#g-list-free"><code class="function">g_list_free()</code></a>.</p></div><div class="refsect1" lang="en"><a name="id3106178"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3106184"></a><h3><a name="GList"></a>GList</h3><a class="indexterm" name="id3106194"></a><pre class="programlisting">typedef struct { gpointer data; GList *next; GList *prev;} GList;</pre><p>The <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a> struct is used for each element in a doubly-linked list.The <em class="structfield"><code>data</code></em> field holds the element's data, which canbe a pointer to any kind of data, or any integer value using the<a href="glib-Type-Conversion-Macros.html" title="Type Conversion Macros">Type Conversion Macros</a>.The <em class="structfield"><code>next</code></em> and <em class="structfield"><code>prev</code></em>pointers are the links to the next and previous elements in the list.</p></div><hr><div class="refsect2" lang="en"><a name="id3106240"></a><h3><a name="g-list-append"></a>g_list_append ()</h3><a class="indexterm" name="id3106251"></a><pre class="programlisting"><a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* g_list_append (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);</pre><p>Adds a new element on to the end of the list.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>The return value is the new start of the list, which may have changed, somake sure you store the new value.</p></div><div class="informalexample"><pre class="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));</pre></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>list</code></em> :</span></td><td>a pointer to a <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>.</td></tr><tr><td><span class="term"><em class="parameter"><code>data</code></em> :</span></td><td>the data for the new element.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new start of the <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3106368"></a><h3><a name="g-list-prepend"></a>g_list_prepend ()</h3><a class="indexterm" name="id3106378"></a><pre class="programlisting"><a href="glib-Doubly-Linked-Lists.html#GList">GList</a>* g_list_prepend (<a href="glib-Doubly-Linked-Lists.html#GList">GList</a> *list, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);</pre><p>Adds a new element on to the start of the list.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>The return value is the new start of the list, which may have changed, somake sure you store the new value.</p></div><div class="informalexample"><pre class="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");</pre></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>list</code></em> :</span></td><td>a pointer to a <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>.</td></tr><tr><td><span class="term"><em class="parameter"><code>data</code></em> :</span></td><td>the data for the new element.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -