Original article address:
Http://hi.baidu.com/study_together/blog/item/f14cb83319f70d94a8018e3e.html
Compile:
Compile: gcc-g-wall-O0 fuck. C-O fuck 'pkg-config -- Libs -- cflags glib-100'
1
Basic operations
Here are some common operations that can be performed using Glist:
# Include < Glib. h >
# Include < Stdio. h >
Int Main ( Int Argc, Char ** Argv ){
Glist * List = NULL;
List = G_list_append (list, " Austin " );
Printf ( " The first item is '% s' \ n " , List -> Data );
List = G_list_insert (list, " Baltimore " , 1 );
Printf ( " The second item is '% s' \ n " , G_list_next (list) -> Data );
List = G_list_remove (list, " Baltimore " );
Printf ( " After removal of 'baltime', the list length is % d \ n " , G_list_length (list ));
Glist * Other_list = G_list_append (null, " Baltimore " );
List = G_list_concat (list, other_list );
Printf ( " After concatenation: " );
G_list_foreach (list, (gfunc) printf, null );
List = G_list_reverse (list );
Printf ( " \ Nafter reversal: " );
G_list_foreach (list, (gfunc) printf, null );
G_list_free (list );
Return 0 ;
}
* ***** Output *****
The first item is 'austin'
The second item is 'baltimore'
After removal of 'baltime', the list length is 1
After concatenation: Austin Baltimore
After reversal: Baltimore Austin
2
Better navigation
After learning about some basic Glist operations, here are some possible operations. The only reason is that each node in Glist has a link to the previous node:
# Include < Glib. h >
# Include < Stdio. h >
Int Main ( Int Argc, Char ** Argv ){
Glist * List = G_list_append (null, " Austin " );
List = G_list_append (list, " Bowie " );
List = G_list_append (list, " Charleston " );
Printf ( " Here's the list: " );
G_list_foreach (list, (gfunc) printf, null );
Glist * Last = G_list_last (list );
Printf ( " \ Nthe first item (using g_list_first) is '% s' \ n " , G_list_first (last) -> Data );
Printf ( " The next-to-last item is '% s' \ n " , G_list_previous (last) -> Data );
Printf ( " The next-to-last item is '% s' \ n " , G_list_nth_prev (last, 1 ) -> Data );
G_list_free (list );
Return 0 ;
}
* ***** Output *****
Here's the list: Austin Bowie Charleston
The first item (using g_list_first) is 'austin'
The next-to-last item is 'bowie'
The next-to-last item is 'bowie'
3
Use the link to delete a node
You have learned how to delete a node from the list while having a pointer to the data it holds. g_list_remove can be done well.
If you have a pointer to the node itself, you can use a quick O (1) operation to directly Delete the node.
# Include < Glib. h >
# Include < Stdio. h >
Int Main ( Int Argc, Char ** Argv ){
Glist * List = G_list_append (null, " Austin " );
List = G_list_append (list, " Bowie " );
List = G_list_append (list, " Chicago " );
Printf ( " Here's the list: " );
G_list_foreach (list, (gfunc) printf, null );
Glist * Bowie = G_list_nth (list, 1 );
List = G_list_remove_link (list, Bowie );
G_list_free_1 (Bowie );
Printf ( " \ Nhere's the list after the remove_link call: " );
G_list_foreach (list, (gfunc) printf, null );
List = G_list_delete_link (list, g_list_nth (list, 1 ));
Printf ( " \ Nhere's the list after the delete_link call: " );
G_list_foreach (list, (gfunc) printf, null );
G_list_free (list );
Return 0 ;
}
* ***** Output *****
Here's the list: Austin Bowie Chicago
Here's the list after the remove_link call: Austin Chicago
Here's the list after the delete_link call: Austin
4
Index and location
If you only want to locate an entry in glist, you have two options. You can use g_list_index, which can be identified by using the data in the entry,
You can also use g_list_position, which uses a pointer to that node. This example shows the two methods:
# Include < Glib. h >
# Include < Stdio. h >
Int Main ( Int Argc, Char ** Argv ){
Glist * List = G_list_append (null, " Austin " );
List = G_list_append (list, " Bowie " );
List = G_list_append (list, " Bowie " );
List = G_list_append (list, " Cheyenne " );
Printf ( " Here's the list: " );
G_list_foreach (list, (gfunc) printf, null );
Printf ( " \ Nitem 'bowie' is located at index % d \ n " , G_list_index (list, " Bowie " ));
Printf ( " Item 'Dallas 'is located at index % d \ n " , G_list_index (list, " Dallas " ));
Glist * Last = G_list_last (list );
Printf ( " Item 'cheyenne' is located at index % d \ n " , G_list_position (list, last ));
G_list_free (list );
Return 0 ;
}
* ***** Output *****
Here's the list: Austin Bowie Cheyenne
Item 'bowie 'is located at Index 1
Item 'Dallas 'is located at index-1
Item 'cheyenne' is located at index 3
End