next up previous contents
Next: Memory allocation Up: GLib Previous: An Overview   Contents

Doubly linked lists

Doubly linked lists are handled by several functions. They allow creating, deleting and modifying user supplied data grouped in lists with dynamic memory allocation. There are also advanced functions for searching and indexing such lists.

The GList structure (given below) and its associated functions provide a standard doubly-linked list data structure.

struct GList
 {
   gpointer data;
   GList *next;
   GList *prev;
 };

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 Singly-Linked Lists which only allow movement through the list in the forward direction).

The data contained in each element can be either integer values, 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 GList 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 GList. NULL is considered to be the empty list so you simply set a GList* to NULL.

To add elements, g_list_append(), g_list_prepend(), g_list_ insert() and g_list_insert_sorted() are used.

To remove elements, g_list_remove() is used.

To find elements in the list g_list_first(), g_list_last(), g_list_ next(), g_list_previous(), g_list_nth(), g_list_nth_data(), g_list_ find() and g_list_find_custom() are used.

To call a function for each element in the list g_list_foreach() could be used.

To free the entire list, g_list_free() is used.


next up previous contents
Next: Memory allocation Up: GLib Previous: An Overview   Contents
Igor Wojnicki 2001-02-21