Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4218312
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: C/C++

2011-07-01 00:12:16

1:SList结构

  1. typedef struct _GSList GSList;

  2. struct _GSList
  3. {
  4. gpointer data;
  5. GSList *next;
  6. };

2: SList 原型

  1. GSList* g_slist_append (GSList *list,
  2.                                           gpointer data) G_GNUC_WARN_UNUSED_RESULT;
  3. GSList* g_slist_prepend (GSList *list,
  4.                                           gpointer data) G_GNUC_WARN_UNUSED_RESULT;
  5. GSList* g_slist_insert (GSList *list,
  6.                                           gpointer data,
  7.                                           gint position) G_GNUC_WARN_UNUSED_RESULT;
  8. GSList* g_slist_insert_sorted (GSList *list,
  9.                                           gpointer data,
  10.                                           GCompareFunc func) G_GNUC_WARN_UNUSED_RESULT;
  11. GSList* g_slist_insert_sorted_with_data (GSList *list,
  12.                                           gpointer data,
  13.                                           GCompareDataFunc func,
  14.                                           gpointer user_data) G_GNUC_WARN_UNUSED_RESULT;
  15. GSList* g_slist_insert_before (GSList *slist,
  16.                                           GSList *sibling,
  17.                                           gpointer data) G_GNUC_WARN_UNUSED_RESULT;
  18. GSList* g_slist_concat (GSList *list1,
  19.                                           GSList *list2) G_GNUC_WARN_UNUSED_RESULT;
  20. GSList* g_slist_remove (GSList *list,
  21.                                           gconstpointer data) G_GNUC_WARN_UNUSED_RESULT;
  22. GSList* g_slist_remove_all (GSList *list,
  23.                                           gconstpointer data) G_GNUC_WARN_UNUSED_RESULT;
  24. GSList* g_slist_remove_link (GSList *list,
  25.                                           GSList *link_) G_GNUC_WARN_UNUSED_RESULT;
  26. GSList* g_slist_delete_link (GSList *list,
  27.                                           GSList *link_) G_GNUC_WARN_UNUSED_RESULT;
  28. GSList* g_slist_reverse (GSList *list) G_GNUC_WARN_UNUSED_RESULT;
  29. GSList* g_slist_copy (GSList *list) G_GNUC_WARN_UNUSED_RESULT;
  30. GSList* g_slist_nth (GSList *list,
  31.                                           guint n);
  32. GSList* g_slist_find (GSList *list,
  33.                                           gconstpointer data);
  34. GSList* g_slist_find_custom (GSList *list,
  35.                                           gconstpointer data,
  36.                                           GCompareFunc func);
  37. gint g_slist_position (GSList *list,
  38.                                           GSList *llink);
  39. gint g_slist_index (GSList *list,
  40.                                           gconstpointer data);
  41. GSList* g_slist_last (GSList *list);
  42. guint g_slist_length (GSList *list);
  43. void g_slist_foreach (GSList *list,
  44.                                           GFunc func,
  45.                                           gpointer user_data);
  46. GSList* g_slist_sort (GSList *list,
  47.                                           GCompareFunc compare_func) G_GNUC_WARN_UNUSED_RESULT;
  48. GSList* g_slist_sort_with_data (GSList *list,
  49.                                           GCompareDataFunc compare_func,
  50.                                           gpointer user_data) G_GNUC_WARN_UNUSED_RESULT;
  51. gpointer g_slist_nth_data (GSList *list,
  52.                                           guint n);
3:SList 实例

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <glib.h>
  5. #include <glib/gprintf.h>

  6. /*
  7.  * g_slist_append
  8.  * g_slist_prepend
  9.  * g_slist_reverse
  10.  */
  11. static void
  12. test_slist_1(void)
  13. {
  14.     GSList *slist = NULL;
  15.     GSList *st;
  16.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};
  17.     gint i;

  18.     for (i = 0; i < 10; i++)
  19. // GSList* g_slist_append(GSList *list, gpointer data);
  20.         slist = g_slist_append(slist, &nums[i]);
  21.     
  22.     g_printf("The result should be: 0,1,2,3,4,5,6,7,8,9\n");
  23.     g_printf("Begin:\n");
  24.     for (i = 0; i < 10; i++) {
  25.         st = g_slist_nth(slist, i);
  26.         g_printf("%d,", *(gint*)st->data);
  27.     }
  28.     g_printf("\nDone\n");

  29. // GSList* g_slist_reverse(GSList *list);
  30.     slist = g_slist_reverse(slist);

  31.     g_printf("The result should be: 9,8,7,6,5,4,3,2,1,0\n");
  32.     g_printf("Begin:\n");
  33.     for (i = 0; i < 10; i++) {
  34.         st = g_slist_nth(slist, i);
  35.         g_printf("%d,", *(gint*)st->data);
  36.     }
  37.     g_printf("\nDone\n");

  38.     for (i = 0; i < 10; i++)
  39. // GSList* g_slist_prepend(GSList *list, gpointer data);
  40.         slist = g_slist_prepend(slist, &nums[i]);

  41.     g_printf("The result should be: 9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0\n");
  42.     g_printf("Begin:\n");
  43.     for (i = 0; i < 20; i++) {
  44.         st = g_slist_nth(slist, i);
  45.         g_printf("%d,", *(gint*)st->data);
  46.     }
  47.     g_printf("\nDone\n");

  48.     g_slist_free(slist);
  49. }

  50. /*
  51.  * g_slist_insert_before
  52.  * g_slist_insert
  53.  * g_slist_nth
  54.  * g_slist_copy
  55.  */
  56. static void
  57. test_slist_2(void)
  58. {
  59.     GSList *slist = NULL;
  60.     GSList *st;
  61.     GSList *st2;
  62.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};
  63.     gint i;
  64. // GSList* g_slist_insert_before(GSList *slist, GSList *sibling, gpointer data);
  65.     slist = g_slist_insert_before(NULL, NULL, &nums[1]);

  66. // GSList* g_slist_insert(GSList *list, gpointer data, gint position);
  67.     slist = g_slist_insert(slist, &nums[3], 1);
  68.     slist = g_slist_insert(slist, &nums[4], -1);
  69.     slist = g_slist_insert(slist, &nums[0], 0);
  70.     slist = g_slist_insert(slist, &nums[5], 100);
  71.     slist = g_slist_insert_before(slist, NULL, &nums[6]);
  72.     slist = g_slist_insert_before(slist, slist->next->next, &nums[2]);

  73.     slist = g_slist_insert(slist, &nums[9], 7);
  74.     slist = g_slist_insert(slist, &nums[8], 7);
  75.     slist = g_slist_insert(slist, &nums[7], 7);

  76.     g_printf("The result should be: 0,1,2,3,4,5,6,7,8,9\n");
  77.     g_printf("Begin:\n");
  78.     for (i = 0; i < 10; i++) {
  79. // GSList* g_slist_nth(GSList *list, guint n);
  80.         st = g_slist_nth(slist, i);
  81.         g_printf("%d,", *(gint*)st->data);
  82.     }
  83.     g_printf("\nDone\n");

  84. // GSList* g_slist_copy(GSList *list);
  85.     st = g_slist_copy(slist);
  86.     g_printf("The result should be: 0,1,2,3,4,5,6,7,8,9\n");
  87.     g_printf("Begin:\n");
  88.     for (i = 0; i < 10; i++) {
  89.         st2 = g_slist_nth(st, i);
  90.         g_printf("%d,", *(gint*)st2->data);
  91.     }
  92.     g_printf("\nDone\n");

  93.     g_slist_free(st);
  94.     g_slist_free(slist);
  95. }

  96. /*
  97.  * g_slist_lenth
  98.  * g_slist_remove
  99.  * g_slist_remove_all
  100.  * g_slist_last
  101.  * g_slist_next
  102.  */
  103. static void
  104. test_slist_3(void)
  105. {
  106.     GSList *slist = NULL;
  107.     GSList *st;
  108.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};
  109.     gint i;

  110.     for (i = 0; i < 10; i++) {
  111.         slist = g_slist_append(slist, &nums[i]);
  112.         slist = g_slist_append(slist, &nums[i]);
  113.     }
  114. // guint g_slist_lenth(GSList *list);
  115.     g_printf("The length should be 20.\nResult:%d\n", g_slist_length(slist));

  116.     for (i = 0; i < 10; i++)
  117. // GSList* g_slist_remove_all(GSList *list, gconstpointer data);
  118.         slist = g_slist_remove_all(slist, &nums[i]);
  119.     g_printf("The length should be 0.\nResult:%d\n", g_slist_length(slist));

  120.     for (i = 0; i < 10; i++) {
  121.         slist = g_slist_append(slist, &nums[i]);
  122.         slist = g_slist_append(slist, &nums[i]);
  123.     }
  124.     for (i = 0; i < 10; i++)
  125. // GSList* g_slist_remove(GSList *list, gconstpointer data);
  126.         slist = g_slist_remove(slist, &nums[i]);
  127.     g_printf("The length should be 10.\nResult:%d\n", g_slist_length(slist));
  128.     g_printf("The first should be 0.\nResult:%d\n", *(gint*)slist->data);
  129. // GSList* g_slist_last(GSList *list);
  130.     g_printf("The last should be 9.\nResult:%d\n", *(gint*)g_slist_last(slist)->data);

  131.     g_printf("The first:");
  132.     g_printf("%d\n", *(gint*)slist->data);    
  133.     st = slist;
  134.     for (i = 0; i < 9; i++) {
  135. // #define g_slist_next (slist)
  136.         st = g_slist_next(st);    
  137.         g_printf("The next:%d\n", *(gint*)st->data);
  138.     }

  139.     g_slist_free(slist);
  140. }

  141. static gint
  142. find_num(gconstpointer l, gconstpointer data)
  143. {
  144.     return *(gint*)l - GPOINTER_TO_INT(data);
  145. }

  146. /*
  147.  * g_slist_index
  148.  * g_slist_position
  149.  * g_slist_find_custom
  150.  */
  151. static void
  152. test_slist_4(void)
  153. {
  154.     GSList *slist = NULL;
  155.     GSList *st;
  156.     gint nums[10] = {0,1,2,3,4,5,6,7,8,9};
  157.     gint i;

  158.     for (i = 0; i < 10; i++) {
  159.         slist = g_slist_append(slist, &nums[i]);
  160.     }
  161. // gint g_slist_index(GSList *list, gconstpointer data);
  162.     g_printf("The index should be -1.\nResult:%d\n", g_slist_index(slist, NULL));
  163. // gint g_slist_position(GSList *list, GSList *llink);
  164.     g_printf("The position should be -1.\nResult:%d\n", g_slist_position(slist, NULL));

  165.     for (i = 0; i < 10; i++) {
  166. // GSList* g_slist_find_custom(GSList *list, gconstpointer data, GCompareFunc func);
  167.         st = g_slist_find_custom(slist, GINT_TO_POINTER(i), find_num);
  168.         g_printf("The position should be %d.\nResult:%d\n", i, g_slist_position(slist, st));
  169.     }

  170.     g_slist_free(slist);
  171. }

  172. #define SIZE        10
  173. #define NUMBER_MAX    99

  174. static guint32 array[SIZE];

  175. static gint
  176. sort(gconstpointer p1, gconstpointer p2)
  177. {
  178.     gint32 a, b;
  179.     a = GPOINTER_TO_INT (p1);
  180.     b = GPOINTER_TO_INT (p2);

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

  183. static gint
  184. sort_r(gconstpointer p1, gconstpointer p2)
  185. {
  186.     gint32 a, b;
  187.     a = GPOINTER_TO_INT (p1);
  188.     b = GPOINTER_TO_INT (p2);

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

  191. /*
  192.  * g_slist_sort
  193.  * g_slist_sort_with_data
  194.  * g_slist_nth_data
  195.  */
  196. static void
  197. test_slist_5(void)
  198. {
  199.     GSList *slist = NULL;
  200.     gint i;

  201.     for (i = 0; i < SIZE; i++)
  202.         slist = g_slist_append(slist, GINT_TO_POINTER(array[i]));

  203. // GSList* g_slist_sort(GSList *list, GCompareFunc compare);
  204.     slist = g_slist_sort(slist, sort);

  205.     g_printf("The result should be sorted.\nResult:");
  206.     for (i = 0; i < SIZE; i++) {
  207. // gpointer g_slist_nth_data(GSList *list, guint n);
  208.         gpointer p = g_slist_nth_data(slist, i);
  209.         g_printf("%d,", GPOINTER_TO_INT(p));
  210.     }
  211.     g_printf("\n");

  212. // GSList* g_slist_sort_with_data(GSList *list, GCompareDataFunc compare_func, gpinter user_data);
  213.     slist = g_slist_sort_with_data(slist, (GCompareDataFunc)sort_r, NULL);
  214.     g_printf("The result should be sorted[reversed].\nResult:");
  215.     for (i = 0; i < SIZE; i++) {
  216.         gpointer p = g_slist_nth_data(slist, i);
  217.         g_printf("%d,", GPOINTER_TO_INT(p));
  218.     }
  219.     g_printf("\n");

  220.     g_slist_free(slist);
  221. }

  222. static void
  223. print(gpointer p1, gpointer p2)
  224. {
  225.     g_printf("%d,", GPOINTER_TO_INT(p1));
  226. }

  227. /*
  228.  * g_slist_insert_sorted
  229.  * g_slist_insert_sorted_with_data
  230.  * g_slist_concat
  231.  * g_slist_foreach
  232.  */
  233. static void
  234. test_slist_6(void)
  235. {
  236.     GSList *slist = NULL;
  237.     GSList *st = NULL;
  238.     GSList *sc = NULL;
  239.     gint i;

  240.     for (i = 0; i < SIZE; i++) {
  241. // GSList* g_slist_insert_sorted(GSList *list, gpointer data, GCompareFunc func);
  242.         slist = g_slist_insert_sorted(slist, GINT_TO_POINTER(array[i]), sort);
  243. // GSList* g_slist_insert_sorted_with_data(GSList *list, gpointer data,
  244. //                            GCompareDataFunc func,
  245. //                            gpinter user_data);
  246.         st = g_slist_insert_sorted_with_data(st, GINT_TO_POINTER(array[i]),
  247.                             (GCompareDataFunc)sort_r,
  248.                             NULL);
  249.     }
  250.     
  251.     g_printf("The result should be sorted.\nResult:");
  252.     for (i = 0; i < SIZE; i++) {
  253.         gpointer p = g_slist_nth_data(slist, i);
  254.         g_printf("%d,", GPOINTER_TO_INT(p));
  255.     }
  256.     g_printf("\n");

  257.     g_printf("The result should be sorted[reversed].\nResult:");
  258.     for (i = 0; i < SIZE; i++) {
  259.         gpointer p = g_slist_nth_data(st, i);
  260.         g_printf("%d,", GPOINTER_TO_INT(p));
  261.     }
  262.     g_printf("\n");

  263. // GSList* g_slist_concat(GSList *list1, *list2);
  264.     sc = g_slist_concat(slist, st);

  265.     g_printf("The result should be concated.\nResult:");
  266. // void g_slist_foreach(GSList *list, GFunc func, gpointer user_data);
  267.     g_slist_foreach(sc, (GFunc)print, NULL);
  268.     g_printf("\n");
  269.     
  270.     g_slist_free(slist);
  271.     g_slist_free(st);

  272. }

  273. int
  274. main(void)
  275. {
  276.     printf("BEGIN:\n************************************************************\n");
  277.     printf("\n------------------------------------------------------------\ntest_slist_1:\n");
  278.     printf("------------------------------------------------------------\n");
  279.     test_slist_1();
  280.     printf("\n------------------------------------------------------------\ntest_slist_2:\n");
  281.     printf("------------------------------------------------------------\n");
  282.     test_slist_2();
  283.     printf("\n------------------------------------------------------------\ntest_slist_3:\n");
  284.     printf("------------------------------------------------------------\n");
  285.     test_slist_3();
  286.     printf("\n------------------------------------------------------------\ntest_slist_4:\n");
  287.     printf("------------------------------------------------------------\n");
  288.     test_slist_4();

  289.     int i;
  290.     srand((unsigned)time(0));
  291.     printf("The init array is:\n");
  292.     for (i = 0; i < SIZE; i++) {
  293.         array[i] = rand() % (NUMBER_MAX+1);
  294.         printf("%d,", array[i]);
  295.     }
  296.     printf("\n");

  297.     printf("\n------------------------------------------------------------\ntest_slist_5:\n");
  298.     printf("------------------------------------------------------------\n");
  299.     test_slist_5();

  300.     printf("\n------------------------------------------------------------\ntest_slist_6:\n");
  301.     printf("------------------------------------------------------------\n");
  302.     test_slist_6();

  303.     printf("\n************************************************************\nDONE\n");

  304.     return 0;
  305. }

阅读(1525) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~