Glib实例学习(2)双链表

1:List 结构

 

 
 
  1. typedef struct {  
  2.   gpointer data;  
  3.   GList *next;  
  4.   GList *prev;  
  5. } GList; 
2:List原型
 
 
  1. GList* g_list_append (GList *list,  
  2.                                                          gpointer data);  
  3. GList* g_list_prepend (GList *list,  
  4.                                                          gpointer data);  
  5. GList* g_list_insert (GList *list,  
  6.                                                          gpointer data,  
  7.                                                          gint position);  
  8. GList* g_list_insert_before (GList *list,  
  9.                                                          GList *sibling,  
  10.                                                          gpointer data);  
  11. GList* g_list_insert_sorted (GList *list,  
  12.                                                          gpointer data,  
  13.                                                          GCompareFunc func);  
  14. GList* g_list_remove (GList *list,  
  15.                                                          gconstpointer data);  
  16. GList* g_list_remove_link (GList *list,  
  17.                                                          GList *llink);  
  18. GList* g_list_delete_link (GList *list,  
  19.                                                          GList *link_);  
  20. GList* g_list_remove_all (GList *list,  
  21.                                                          gconstpointer data);  
  22. void g_list_free (GList *list);  
  23.  
  24. GList* g_list_alloc (void);  
  25. void g_list_free_1 (GList *list);  
  26. #define g_list_free1  
  27.  
  28. guint g_list_length (GList *list);  
  29. GList* g_list_copy (GList *list);  
  30. GList* g_list_reverse (GList *list);  
  31. GList* g_list_sort (GList *list,  
  32.                                                          GCompareFunc compare_func);  
  33. gint (*GCompareFunc) (gconstpointer a,  
  34.                                                          gconstpointer b);  
  35. GList* g_list_insert_sorted_with_data (GList *list,  
  36.                                                          gpointer data,  
  37.                                                          GCompareDataFunc func,  
  38.                                                          gpointer user_data);  
  39. GList* g_list_sort_with_data (GList *list,  
  40.                                                          GCompareDataFunc compare_func,  
  41.                                                          gpointer user_data);  
  42. gint (*GCompareDataFunc) (gconstpointer a,  
  43.                                                          gconstpointer b,  
  44.                                                          gpointer user_data);  
  45. GList* g_list_concat (GList *list1,  
  46.                                                          GList *list2);  
  47. void g_list_foreach (GList *list,  
  48.                                                          GFunc func,  
  49.                                                          gpointer user_data);  
  50. void (*GFunc) (gpointer data,  
  51.                                                          gpointer user_data);  
  52.  
  53. GList* g_list_first (GList *list);  
  54. GList* g_list_last (GList *list);  
  55. #define g_list_previous (list)  
  56. #define g_list_next (list)  
  57. GList* g_list_nth (GList *list,  
  58.                                                          guint n);  
  59. gpointer g_list_nth_data (GList *list,  
  60.                                                          guint n);  
  61. GList* g_list_nth_prev (GList *list,  
  62.                                                          guint n);  
  63.  
  64. GList* g_list_find (GList *list,  
  65.                                                          gconstpointer data);  
  66. GList* g_list_find_custom (GList *list,  
  67.                                                          gconstpointer data,  
  68.                                                          GCompareFunc func);  
  69. gint g_list_position (GList *list,  
  70.                                                          GList *llink);  
  71. gint g_list_index (GList *list,  
  72.                                                          gconstpointer data); 
3:List实例
 
 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <glib.h>  
  4. #include <glib/gprintf.h>  
  5.  
  6. static gint  
  7. sort(gconstpointer p1, gconstpointer p2)  
  8. {  
  9.     gint32 a, b;  
  10.       
  11.     a = GPOINTER_TO_INT(p1);  
  12.     b = GPOINTER_TO_INT(p2);  
  13.  
  14.     return (a > b ? +1 : a == b ? 0 : -1);  
  15. }  
  16.  
  17. static gint  
  18. sort_r(gconstpointer p1, gconstpointer p2)  
  19. {  
  20.     gint32 a, b;  
  21.       
  22.     a = GPOINTER_TO_INT(p1);  
  23.     b = GPOINTER_TO_INT(p2);  
  24.  
  25.     return (a < b ? +1 : a == b ? 0 : -1);  
  26. }  
  27.  
  28. static void 
  29. print(gpointer p1, gpointer p2)  
  30. {  
  31.     g_printf("%d,", *(gint*)p1);  
  32. }  
  33.  
  34. static void 
  35. test_list(void)  
  36. {  
  37.     GList *list = NULL;  
  38.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};  
  39.  
  40. // GList* g_list_append(GList *list, gpointer data);  
  41.     list = g_list_append(list, &nums[1]);  
  42.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)list->data);  
  43. // GList* g_list_prepend(GList *list, gpointer data);  
  44.     list = g_list_prepend(list, &nums[0]);  
  45. // GList* g_list_first(GList *list);  
  46.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_first(list)->data);  
  47. // GList* g_list_insert(GList *list, gpointer data, gint position);  
  48.     list = g_list_insert(list, &nums[2], 2);  
  49. // GList* g_list_last(GList *list);  
  50.     g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[2], *(gint*)g_list_last(list)->data);  
  51. // GList* g_list_insert_before(GList *list, GList *sibling, gpointer data);  
  52.     list = g_list_insert_before(list, NULL, &nums[3]);  
  53.     g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[3], *(gint*)g_list_last(list)->data);  
  54. // #define g_list_next (list)  
  55.     g_printf("The second item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)g_list_next(list)->data);  
  56. // #define g_list_previous (list)  
  57.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_previous(g_list_next(list))->data);  
  58. // gint g_list_index(GList *list, gconstpointer data);  
  59.     g_printf("The index of '%d' should be '%d' now.\t\tResult: %d.\n", nums[2], 2, g_list_index(list, &nums[2]));  
  60. // gint g_list_position(GList *list, GList *llink);  
  61.     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));  
  62. // guint g_list_length(GList *list);  
  63.     g_printf("The length of list should be 4 now.\t\tResult: %d.\n", g_list_length(list));  
  64.       
  65.     GList *lt = NULL;  
  66.     gint i;  
  67.       
  68. // GList* g_list_insert_sorted(GList *list, gpointer data, GCompareFunc func);  
  69.     for (i = 4; i < 10; i++)  
  70.         lt = g_list_insert_sorted(lt, &nums[i], sort_r);  
  71. // GList* g_list_reverse(GList *list);  
  72.     lt = g_list_reverse(lt);  
  73.       
  74.     g_printf("The second half of list should be sored now.\nResult:");  
  75. // gpointer g_list_nth_data(GList *list, guint n);  
  76.     for (i = 4; i < 10; i++)  
  77.         g_printf("%d,",*(gint*)(g_list_nth_data(lt, i-4)));  
  78.     g_printf("\n");  
  79.  
  80. // GList* g_list_concat(GList *list1, GList *list2);  
  81.     list = g_list_concat(list, lt);  
  82.     g_printf("The list should have all items which should be sored now.\nResult:");  
  83. // void g_list_foreach(GList *list, GFunc func, gpointer user_data);  
  84.     g_list_foreach(list, print, NULL);  
  85.     g_printf("\n");  
  86.  
  87. // GList* g_list_sort(GList *list, GCompareFunc compare_func);  
  88.     list = g_list_sort(list, sort_r);  
  89.     g_printf("The list should have all items which should be sored reversed now.\nResult:");  
  90.     g_list_foreach(list, print, NULL);  
  91.     g_printf("\n");  
  92.  
  93.     GList *lb = NULL;  
  94. // GList* g_list_copy(GList *list);  
  95.     lb = g_list_copy(list);  
  96.     g_printf("The backup list should have the same item and sequence now.\nResult:");  
  97. // GList* g_list_nth(GList *list, guint n);  
  98.     for (i = 0; i < 10; i++) {  
  99.         GList *ltmp = g_list_nth(lb, i);  
  100.         g_printf("%d,", *(gint*)ltmp->data);  
  101.     }  
  102.     g_printf("\n");  
  103.  
  104. // GList* g_list_sort_with_data(GList *list, GCompareDataFunc compare_func, gpointer user_data);  
  105.     lb = g_list_sort_with_data(lb, (GCompareDataFunc)sort, NULL);  
  106.     g_printf("The backup list should have all items which should be sored now.\nResult:");  
  107.     g_list_foreach(lb, print, NULL);  
  108.     g_printf("\n");  
  109.  
  110.     GList *lall = NULL;  
  111.     lall = g_list_concat(list, lb);  
  112.     g_printf("The concated list should have all items now.\nResult:");  
  113.     g_list_foreach(lall, print, NULL);  
  114.     g_printf("\n");  
  115.  
  116. // GList* g_list_remove(GList *list, gconstpointer data);  
  117.     lall = g_list_remove(lall, &nums[0]);  
  118.     g_printf("The list should have only one '%d' item now.\nResult:", nums[0]);  
  119.     g_list_foreach(lall, print, NULL);  
  120.     g_printf("\n");  
  121.  
  122. // GList* g_list_remove_all(GList *list, gconstpointer data);  
  123.     lall = g_list_remove_all(lall, &nums[9]);  
  124.     g_printf("The list should not have '%d' item now.\nResult:", nums[9]);  
  125.     g_list_foreach(lall, print, NULL);  
  126.     g_printf("\n");  
  127.       
  128.     GList *ll = NULL;  
  129. // GList* g_list_find(GList *list, gconstpointer data);  
  130.     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);  
  131. // GList* g_list_find_custom(GList *list, gconstpointer data, GCompareFunc func);  
  132.     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);  
  133.  
  134. // void g_list_free(GList *list);  
  135.     g_list_free(lall);  
  136. }  
  137.  
  138. int 
  139. main(void)  
  140. {  
  141.     printf("BEGIN:\n************************************************************\n");  
  142.     test_list();  
  143.     printf("\n************************************************************\nDONE\n");  
  144.     return 0;  

4:编译

 
 
  1. gcc `pkg-config --cflags --libs glib-2.0` -Wall -O2 -o list list.c 
5:结果
 
 
  1. BEGIN:  
  2. ************************************************************  
  3. The first item should be '1' now. Result: 1.  
  4. The first item should be '0' now. Result: 0.  
  5. The last item should be '2' now. Result: 2.  
  6. The last item should be '3' now. Result: 3.  
  7. The second item should be '1' now. Result: 1.  
  8. The first item should be '0' now. Result: 0.  
  9. The index of '2' should be '2' now. Result: 2.  
  10. The position of the third item should be 2 now. Result: 2.  
  11. The length of list should be 4 now. Result: 4.  
  12. The second half of list should be sored now.  
  13. Result:4,5,6,7,8,9,  
  14. The list should have all items which should be sored now.  
  15. Result:0,1,2,3,4,5,6,7,8,9,  
  16. The list should have all items which should be sored reversed now.  
  17. Result:9,8,7,6,5,4,3,2,1,0,  
  18. The backup list should have the same item and sequence now.  
  19. Result:9,8,7,6,5,4,3,2,1,0,  
  20. The backup list should have all items which should be sored now.  
  21. Result:0,1,2,3,4,5,6,7,8,9,  
  22. The concated list should have all items now.  
  23. Result:9,8,7,6,5,4,3,2,1,0,0,1,2,3,4,5,6,7,8,9,  
  24. The list should have only one '0' item now.  
  25. Result:9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9,  
  26. The list should not have '9' item now.  
  27. Result:8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,  
  28. The list should find '0' now. Resutl: 0.  
  29. The list should not find '9' now. Resutl: -1.  
  30.  
  31. ************************************************************  
  32. DONE 
转载自:http://blog.chinaunix.net/space.php?uid=25696269&do=blog&id=599214

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值