1:List 结构
2:List原型
- typedef struct {
- gpointer data;
- GList *next;
- GList *prev;
- } GList;
3:List实例
- GList* g_list_append (GList *list,
- gpointer data);
- GList* g_list_prepend (GList *list,
- gpointer data);
- GList* g_list_insert (GList *list,
- gpointer data,
- gint position);
- GList* g_list_insert_before (GList *list,
- GList *sibling,
- gpointer data);
- GList* g_list_insert_sorted (GList *list,
- gpointer data,
- GCompareFunc func);
- GList* g_list_remove (GList *list,
- gconstpointer data);
- GList* g_list_remove_link (GList *list,
- GList *llink);
- GList* g_list_delete_link (GList *list,
- GList *link_);
- GList* g_list_remove_all (GList *list,
- gconstpointer data);
- void g_list_free (GList *list);
- GList* g_list_alloc (void);
- void g_list_free_1 (GList *list);
- #define g_list_free1
- guint g_list_length (GList *list);
- GList* g_list_copy (GList *list);
- GList* g_list_reverse (GList *list);
- GList* g_list_sort (GList *list,
- GCompareFunc compare_func);
- gint (*GCompareFunc) (gconstpointer a,
- gconstpointer b);
- GList* g_list_insert_sorted_with_data (GList *list,
- gpointer data,
- GCompareDataFunc func,
- gpointer user_data);
- GList* g_list_sort_with_data (GList *list,
- GCompareDataFunc compare_func,
- gpointer user_data);
- gint (*GCompareDataFunc) (gconstpointer a,
- gconstpointer b,
- gpointer user_data);
- GList* g_list_concat (GList *list1,
- GList *list2);
- void g_list_foreach (GList *list,
- GFunc func,
- gpointer user_data);
- void (*GFunc) (gpointer data,
- gpointer user_data);
- GList* g_list_first (GList *list);
- GList* g_list_last (GList *list);
- #define g_list_previous (list)
- #define g_list_next (list)
- GList* g_list_nth (GList *list,
- guint n);
- gpointer g_list_nth_data (GList *list,
- guint n);
- GList* g_list_nth_prev (GList *list,
- guint n);
- GList* g_list_find (GList *list,
- gconstpointer data);
- GList* g_list_find_custom (GList *list,
- gconstpointer data,
- GCompareFunc func);
- gint g_list_position (GList *list,
- GList *llink);
- gint g_list_index (GList *list,
- gconstpointer data);
- #include <stdio.h>
- #include <stdlib.h>
- #include <glib.h>
- #include <glib/gprintf.h>
- static gint
- sort(gconstpointer p1, gconstpointer p2)
- {
- gint32 a, b;
- a = GPOINTER_TO_INT(p1);
- b = GPOINTER_TO_INT(p2);
- return (a > b ? +1 : a == b ? 0 : -1);
- }
- static gint
- sort_r(gconstpointer p1, gconstpointer p2)
- {
- gint32 a, b;
- a = GPOINTER_TO_INT(p1);
- b = GPOINTER_TO_INT(p2);
- return (a < b ? +1 : a == b ? 0 : -1);
- }
- static void
- print(gpointer p1, gpointer p2)
- {
- g_printf("%d,", *(gint*)p1);
- }
- static void
- test_list(void)
- {
- GList *list = NULL;
- gint nums[10] = {0,1,2,3,4,5,6,7,8,9};
- // GList* g_list_append(GList *list, gpointer data);
- list = g_list_append(list, &nums[1]);
- g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)list->data);
- // GList* g_list_prepend(GList *list, gpointer data);
- list = g_list_prepend(list, &nums[0]);
- // GList* g_list_first(GList *list);
- g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_first(list)->data);
- // GList* g_list_insert(GList *list, gpointer data, gint position);
- list = g_list_insert(list, &nums[2], 2);
- // GList* g_list_last(GList *list);
- g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[2], *(gint*)g_list_last(list)->data);
- // GList* g_list_insert_before(GList *list, GList *sibling, gpointer data);
- list = g_list_insert_before(list, NULL, &nums[3]);
- g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[3], *(gint*)g_list_last(list)->data);
- // #define g_list_next (list)
- g_printf("The second item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)g_list_next(list)->data);
- // #define g_list_previous (list)
- g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_previous(g_list_next(list))->data);
- // gint g_list_index(GList *list, gconstpointer data);
- g_printf("The index of '%d' should be '%d' now.\t\tResult: %d.\n", nums[2], 2, g_list_index(list, &nums[2]));
- // gint g_list_position(GList *list, GList *llink);
- g_printf("The position of the third item should be 2 now.\t\tResult: %d.\n", g_list_position(list, g_list_next(list)->next));
- // guint g_list_length(GList *list);
- g_printf("The length of list should be 4 now.\t\tResult: %d.\n", g_list_length(list));
- GList *lt = NULL;
- gint i;
- // GList* g_list_insert_sorted(GList *list, gpointer data, GCompareFunc func);
- for (i = 4; i < 10; i++)
- lt = g_list_insert_sorted(lt, &nums[i], sort_r);
- // GList* g_list_reverse(GList *list);
- lt = g_list_reverse(lt);
- g_printf("The second half of list should be sored now.\nResult:");
- // gpointer g_list_nth_data(GList *list, guint n);
- for (i = 4; i < 10; i++)
- g_printf("%d,",*(gint*)(g_list_nth_data(lt, i-4)));
- g_printf("\n");
- // GList* g_list_concat(GList *list1, GList *list2);
- list = g_list_concat(list, lt);
- g_printf("The list should have all items which should be sored now.\nResult:");
- // void g_list_foreach(GList *list, GFunc func, gpointer user_data);
- g_list_foreach(list, print, NULL);
- g_printf("\n");
- // GList* g_list_sort(GList *list, GCompareFunc compare_func);
- list = g_list_sort(list, sort_r);
- g_printf("The list should have all items which should be sored reversed now.\nResult:");
- g_list_foreach(list, print, NULL);
- g_printf("\n");
- GList *lb = NULL;
- // GList* g_list_copy(GList *list);
- lb = g_list_copy(list);
- g_printf("The backup list should have the same item and sequence now.\nResult:");
- // GList* g_list_nth(GList *list, guint n);
- for (i = 0; i < 10; i++) {
- GList *ltmp = g_list_nth(lb, i);
- g_printf("%d,", *(gint*)ltmp->data);
- }
- g_printf("\n");
- // GList* g_list_sort_with_data(GList *list, GCompareDataFunc compare_func, gpointer user_data);
- lb = g_list_sort_with_data(lb, (GCompareDataFunc)sort, NULL);
- g_printf("The backup list should have all items which should be sored now.\nResult:");
- g_list_foreach(lb, print, NULL);
- g_printf("\n");
- GList *lall = NULL;
- lall = g_list_concat(list, lb);
- g_printf("The concated list should have all items now.\nResult:");
- g_list_foreach(lall, print, NULL);
- g_printf("\n");
- // GList* g_list_remove(GList *list, gconstpointer data);
- lall = g_list_remove(lall, &nums[0]);
- g_printf("The list should have only one '%d' item now.\nResult:", nums[0]);
- g_list_foreach(lall, print, NULL);
- g_printf("\n");
- // GList* g_list_remove_all(GList *list, gconstpointer data);
- lall = g_list_remove_all(lall, &nums[9]);
- g_printf("The list should not have '%d' item now.\nResult:", nums[9]);
- g_list_foreach(lall, print, NULL);
- g_printf("\n");
- GList *ll = NULL;
- // GList* g_list_find(GList *list, gconstpointer data);
- g_printf("The list should find '%d' now.\t\tResutl: %d.\n", nums[0], (ll = g_list_find(lall, &nums[0])) ? *(gint*)ll->data : -1);
- // GList* g_list_find_custom(GList *list, gconstpointer data, GCompareFunc func);
- g_printf("The list should not find '%d' now.\t\tResutl: %d.\n", nums[9], (ll = g_list_find_custom(lall, &nums[9], sort)) ? *(gint*)ll->data : -1);
- // void g_list_free(GList *list);
- g_list_free(lall);
- }
- int
- main(void)
- {
- printf("BEGIN:\n************************************************************\n");
- test_list();
- printf("\n************************************************************\nDONE\n");
- return 0;
- }
4:编译
5:结果
- gcc `pkg-config --cflags --libs glib-2.0` -Wall -O2 -o list list.c
转载自:http://blog.chinaunix.net/space.php?uid=25696269&do=blog&id=599214
- BEGIN:
- ************************************************************
- The first item should be '1' now. Result: 1.
- The first item should be '0' now. Result: 0.
- The last item should be '2' now. Result: 2.
- The last item should be '3' now. Result: 3.
- The second item should be '1' now. Result: 1.
- The first item should be '0' now. Result: 0.
- The index of '2' should be '2' now. Result: 2.
- The position of the third item should be 2 now. Result: 2.
- The length of list should be 4 now. Result: 4.
- The second half of list should be sored now.
- Result:4,5,6,7,8,9,
- The list should have all items which should be sored now.
- Result:0,1,2,3,4,5,6,7,8,9,
- The list should have all items which should be sored reversed now.
- Result:9,8,7,6,5,4,3,2,1,0,
- The backup list should have the same item and sequence now.
- Result:9,8,7,6,5,4,3,2,1,0,
- The backup list should have all items which should be sored now.
- Result:0,1,2,3,4,5,6,7,8,9,
- The concated list should have all items now.
- Result:9,8,7,6,5,4,3,2,1,0,0,1,2,3,4,5,6,7,8,9,
- The list should have only one '0' item now.
- Result:9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9,
- The list should not have '9' item now.
- Result:8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,
- The list should find '0' now. Resutl: 0.
- The list should not find '9' now. Resutl: -1.
- ************************************************************
- DONE