Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15862
  • 博文数量: 2
  • 博客积分: 26
  • 博客等级: 民兵
  • 技术积分: 22
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-18 13:22
文章分类
文章存档

2013年(2)

我的朋友
最近访客

分类:

2013-01-04 17:45:54

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. GList* g_list_alloc (void);
  24. void g_list_free_1 (GList *list);
  25. #define g_list_free1

  26. guint g_list_length (GList *list);
  27. GList* g_list_copy (GList *list);
  28. GList* g_list_reverse (GList *list);
  29. GList* g_list_sort (GList *list,
  30.                                                          GCompareFunc compare_func);
  31. gint (*GCompareFunc) (gconstpointer a,
  32.                                                          gconstpointer b);
  33. GList* g_list_insert_sorted_with_data (GList *list,
  34.                                                          gpointer data,
  35.                                                          GCompareDataFunc func,
  36.                                                          gpointer user_data);
  37. GList* g_list_sort_with_data (GList *list,
  38.                                                          GCompareDataFunc compare_func,
  39.                                                          gpointer user_data);
  40. gint (*GCompareDataFunc) (gconstpointer a,
  41.                                                          gconstpointer b,
  42.                                                          gpointer user_data);
  43. GList* g_list_concat (GList *list1,
  44.                                                          GList *list2);
  45. void g_list_foreach (GList *list,
  46.                                                          GFunc func,
  47.                                                          gpointer user_data);
  48. void (*GFunc) (gpointer data,
  49.                                                          gpointer user_data);

  50. GList* g_list_first (GList *list);
  51. GList* g_list_last (GList *list);
  52. #define g_list_previous (list)
  53. #define g_list_next (list)
  54. GList* g_list_nth (GList *list,
  55.                                                          guint n);
  56. gpointer g_list_nth_data (GList *list,
  57.                                                          guint n);
  58. GList* g_list_nth_prev (GList *list,
  59.                                                          guint n);

  60. GList* g_list_find (GList *list,
  61.                                                          gconstpointer data);
  62. GList* g_list_find_custom (GList *list,
  63.                                                          gconstpointer data,
  64.                                                          GCompareFunc func);
  65. gint g_list_position (GList *list,
  66.                                                          GList *llink);
  67. gint g_list_index (GList *list,
  68.                                                          gconstpointer data);
3:List实例
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <glib.h>
  4. #include <glib/gprintf.h>

  5. static gint
  6. sort(gconstpointer p1, gconstpointer p2)
  7. {
  8.     gint32 a, b;
  9.     
  10.     a = GPOINTER_TO_INT(p1);
  11.     b = GPOINTER_TO_INT(p2);

  12.     return (a > b ? +1 : a == b ? 0 : -1);
  13. }

  14. static gint
  15. sort_r(gconstpointer p1, gconstpointer p2)
  16. {
  17.     gint32 a, b;
  18.     
  19.     a = GPOINTER_TO_INT(p1);
  20.     b = GPOINTER_TO_INT(p2);

  21.     return (a < b ? +1 : a == b ? 0 : -1);
  22. }

  23. static void
  24. print(gpointer p1, gpointer p2)
  25. {
  26.     g_printf("%d,", *(gint*)p1);
  27. }

  28. static void
  29. test_list(void)
  30. {
  31.     GList *list = NULL;
  32.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};

  33. // GList* g_list_append(GList *list, gpointer data);
  34.     list = g_list_append(list, &nums[1]);
  35.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)list->data);
  36. // GList* g_list_prepend(GList *list, gpointer data);
  37.     list = g_list_prepend(list, &nums[0]);
  38. // GList* g_list_first(GList *list);
  39.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_first(list)->data);
  40. // GList* g_list_insert(GList *list, gpointer data, gint position);
  41.     list = g_list_insert(list, &nums[2], 2);
  42. // GList* g_list_last(GList *list);
  43.     g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[2], *(gint*)g_list_last(list)->data);
  44. // GList* g_list_insert_before(GList *list, GList *sibling, gpointer data);
  45.     list = g_list_insert_before(list, NULL, &nums[3]);
  46.     g_printf("The last item should be '%d' now.\t\tResult: %d.\n", nums[3], *(gint*)g_list_last(list)->data);
  47. // #define g_list_next (list)
  48.     g_printf("The second item should be '%d' now.\t\tResult: %d.\n", nums[1], *(gint*)g_list_next(list)->data);
  49. // #define g_list_previous (list)
  50.     g_printf("The first item should be '%d' now.\t\tResult: %d.\n", nums[0], *(gint*)g_list_previous(g_list_next(list))->data);
  51. // gint g_list_index(GList *list, gconstpointer data);
  52.     g_printf("The index of '%d' should be '%d' now.\t\tResult: %d.\n", nums[2], 2, g_list_index(list, &nums[2]));
  53. // gint g_list_position(GList *list, GList *llink);
  54.     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));
  55. // guint g_list_length(GList *list);
  56.     g_printf("The length of list should be 4 now.\t\tResult: %d.\n", g_list_length(list));
  57.     
  58.     GList *lt = NULL;
  59.     gint i;
  60.     
  61. // GList* g_list_insert_sorted(GList *list, gpointer data, GCompareFunc func);
  62.     for (i = 4; i < 10; i++)
  63.         lt = g_list_insert_sorted(lt, &nums[i], sort_r);
  64. // GList* g_list_reverse(GList *list);
  65.     lt = g_list_reverse(lt);
  66.     
  67.     g_printf("The second half of list should be sored now.\nResult:");
  68. // gpointer g_list_nth_data(GList *list, guint n);
  69.     for (i = 4; i < 10; i++)
  70.         g_printf("%d,",*(gint*)(g_list_nth_data(lt, i-4)));
  71.     g_printf("\n");

  72. // GList* g_list_concat(GList *list1, GList *list2);
  73.     list = g_list_concat(list, lt);
  74.     g_printf("The list should have all items which should be sored now.\nResult:");
  75. // void g_list_foreach(GList *list, GFunc func, gpointer user_data);
  76.     g_list_foreach(list, print, NULL);
  77.     g_printf("\n");

  78. // GList* g_list_sort(GList *list, GCompareFunc compare_func);
  79.     list = g_list_sort(list, sort_r);
  80.     g_printf("The list should have all items which should be sored reversed now.\nResult:");
  81.     g_list_foreach(list, print, NULL);
  82.     g_printf("\n");

  83.     GList *lb = NULL;
  84. // GList* g_list_copy(GList *list);
  85.     lb = g_list_copy(list);
  86.     g_printf("The backup list should have the same item and sequence now.\nResult:");
  87. // GList* g_list_nth(GList *list, guint n);
  88.     for (i = 0; i < 10; i++) {
  89.         GList *ltmp = g_list_nth(lb, i);
  90.         g_printf("%d,", *(gint*)ltmp->data);
  91.     }
  92.     g_printf("\n");

  93. // GList* g_list_sort_with_data(GList *list, GCompareDataFunc compare_func, gpointer user_data);
  94.     lb = g_list_sort_with_data(lb, (GCompareDataFunc)sort, NULL);
  95.     g_printf("The backup list should have all items which should be sored now.\nResult:");
  96.     g_list_foreach(lb, print, NULL);
  97.     g_printf("\n");

  98.     GList *lall = NULL;
  99.     lall = g_list_concat(list, lb);
  100.     g_printf("The concated list should have all items now.\nResult:");
  101.     g_list_foreach(lall, print, NULL);
  102.     g_printf("\n");

  103. // GList* g_list_remove(GList *list, gconstpointer data);
  104.     lall = g_list_remove(lall, &nums[0]);
  105.     g_printf("The list should have only one '%d' item now.\nResult:", nums[0]);
  106.     g_list_foreach(lall, print, NULL);
  107.     g_printf("\n");

  108. // GList* g_list_remove_all(GList *list, gconstpointer data);
  109.     lall = g_list_remove_all(lall, &nums[9]);
  110.     g_printf("The list should not have '%d' item now.\nResult:", nums[9]);
  111.     g_list_foreach(lall, print, NULL);
  112.     g_printf("\n");
  113.     
  114.     GList *ll = NULL;
  115. // GList* g_list_find(GList *list, gconstpointer data);
  116.     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);
  117. // GList* g_list_find_custom(GList *list, gconstpointer data, GCompareFunc func);
  118.     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);

  119. // void g_list_free(GList *list);
  120.     g_list_free(lall);
  121. }

  122. int
  123. main(void)
  124. {
  125.     printf("BEGIN:\n************************************************************\n");
  126.     test_list();
  127.     printf("\n************************************************************\nDONE\n");
  128.     return 0;
  129. }
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. DONE


阅读(1270) | 评论(0) | 转发(0) |
0

上一篇:Glib实例学习(1)单链表

下一篇:没有了

给主人留下些什么吧!~~