In the previous article I introduced the use of one-way linked list in the GLib library, this article introduces the use of the glib library doubly linked list, or the style of the previous article, using the code to add comments to explain the code, finally posted the results of the program, and then a small number of instructions.
The difference between a doubly linked list and a one-way list is that, from a node, not only can it access its next node, it can also access its previous node, which is defined as follows:
struct GList
{
Gpointer data;
GList *next;
GList *prev;
};
Here you can see that the doubly linked list has one more pointer to the previous node than the one-way list prev. Here is a demo code for a doubly linked list:
/*
* FILE:G_LIST.C
* Desc: This file demonstrates the use of the bidirectional list glist in the glib library
* Compile:gcc-o g_list g_list.c ' pkg-config--cflags--libs glib-2.0 '
*/
#include<Glib.h>
voidDisplay_list (GList*List
{
GList*It=NULL;
/*Traversing a linked list*/
For(It=List It It=It-Next) {
printf"%s", it-data);
}
printf"/n");
}
IntMainIntargcChar*Argv[])
{
GList*List=NULL;
/*
* Append nodes to the tail of the list
*/
List=G_list_append (list,"One");
List=G_list_append (list,"Both");
List=G_list_append (list,"Three");
List=G_list_append (list,"Four");
List=G_list_append (list,"Five");
Display_list (list);
/*Insert in list header*/
List=G_list_prepend (list,"Zero");
Display_list (list);
GList*It=NULL;
/*Finding nodes in a linked list*/
It=G_list_find (list,"Both");
printf"Find data "both":%s/n", it-data);
IntIndex=0;
/*To determine the number of nodes in the linked list that the linked list pointer points to*/
Index=G_list_position (list, it);
printf"Index of "Both" is:%d/n", index);
It=G_list_nth (list,1);
printf"%s/n", it-data);
/*Remove a node from a list*/
printf"After remove data "three", the list:/n");
List=G_list_remove (list,"Three");
Display_list (list);
/*Inserting a node into the list*/
printf"After insert string ' Insert ' at index 3, the list:/n");
List=G_list_insert (list,"INSERT",3);
Display_list (list);
/*Iterating through linked list nodes with internal loops*/
G_list_foreach (list, (Gfunc) printf, NULL);
printf"/n");
GList*Last=NULL;
/*Fetch the last node of the list*/
last = g_list_last (list);
/* From the last node, go through the list */
< Span style= "color: #0000ff;" >for (It = last; it; it = it->prev) { printf ("%s", it->data); } printf ("/n"); /* destroy list */ g_list_free (list); return 0;}
Under Linux, compile with the following command:
Gcc-o g_list g_list.c ' pkg-config--cflags--libs glib-2.0 '
the results of the post-compilation program run are as follows:
$./g_list
One three four five
Zero One, three four five
Find Data "both":
index of "Is:2 "
One
After remove data "Three", the list:
Zero One, four five
After insert string ' Insert ' at index 3, the list:
Zero One, INSERT four five
Zero One, INSERT four five
five four INSERT one zero
Now you should know why I put a space behind each string.
Summary: In the glib library, the use of the doubly linked list is basically the same as the one-way list, just a pointer to the previous node prev. For your application if the one-way list is sufficient, there is no need to use a doubly linked list, as there is an increase in the overhead of maintaining a pointer.
GLib library doubly linked list Glist introduction