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

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类:

2011-07-13 19:41:58

1:结构

  1. typedef struct {
  2.   GList *head;
  3.   GList *tail;
  4.   guint length;
  5. } GQueue;

2:原型

  1. GQueue* g_queue_new (void);
  2. void g_queue_free (GQueue *queue);
  3. #define G_QUEUE_INIT
  4. void g_queue_init (GQueue *queue);
  5. void g_queue_clear (GQueue *queue);
  6. gboolean g_queue_is_empty (GQueue *queue);
  7. guint g_queue_get_length (GQueue *queue);
  8. void g_queue_reverse (GQueue *queue);
  9. GQueue* g_queue_copy (GQueue *queue);
  10. void g_queue_foreach (GQueue *queue,
  11.                                                          GFunc func,
  12.                                                          gpointer user_data);
  13. GList* g_queue_find (GQueue *queue,
  14.                                                          gconstpointer data);
  15. GList* g_queue_find_custom (GQueue *queue,
  16.                                                          gconstpointer data,
  17.                                                          GCompareFunc func);
  18. void g_queue_sort (GQueue *queue,
  19.                                                          GCompareDataFunc compare_func,
  20.                                                          gpointer user_data);
  21. void g_queue_push_head (GQueue *queue,
  22.                                                          gpointer data);
  23. void g_queue_push_tail (GQueue *queue,
  24.                                                          gpointer data);
  25. void g_queue_push_nth (GQueue *queue,
  26.                                                          gpointer data,
  27.                                                          gint n);
  28. gpointer g_queue_pop_head (GQueue *queue);
  29. gpointer g_queue_pop_tail (GQueue *queue);
  30. gpointer g_queue_pop_nth (GQueue *queue,
  31.                                                          guint n);
  32. gpointer g_queue_peek_head (GQueue *queue);
  33. gpointer g_queue_peek_tail (GQueue *queue);
  34. gpointer g_queue_peek_nth (GQueue *queue,
  35.                                                          guint n);
  36. gint g_queue_index (GQueue *queue,
  37.                                                          gconstpointer data);
  38. void g_queue_remove (GQueue *queue,
  39.                                                          gconstpointer data);
  40. void g_queue_remove_all (GQueue *queue,
  41.                                                          gconstpointer data);
  42. void g_queue_insert_before (GQueue *queue,
  43.                                                          GList *sibling,
  44.                                                          gpointer data);
  45. void g_queue_insert_after (GQueue *queue,
  46.                                                          GList *sibling,
  47.                                                          gpointer data);
  48. void g_queue_insert_sorted (GQueue *queue,
  49.                                                          gpointer data,
  50.                                                          GCompareDataFunc func,
  51.                                                          gpointer user_data);
  52. void g_queue_push_head_link (GQueue *queue,
  53.                                                          GList *link_);
  54. void g_queue_push_tail_link (GQueue *queue,
  55.                                                          GList *link_);
  56. void g_queue_push_nth_link (GQueue *queue,
  57.                                                          gint n,
  58.                                                          GList *link_);
  59. GList* g_queue_pop_head_link (GQueue *queue);
  60. GList* g_queue_pop_tail_link (GQueue *queue);
  61. GList* g_queue_pop_nth_link (GQueue *queue,
  62.                                                          guint n);
  63. GList* g_queue_peek_head_link (GQueue *queue);
  64. GList* g_queue_peek_tail_link (GQueue *queue);
  65. GList* g_queue_peek_nth_link (GQueue *queue,
  66.                                                          guint n);
  67. gint g_queue_link_index (GQueue *queue,
  68.                                                          GList *link_);
  69. void g_queue_unlink (GQueue *queue,
  70.                                                          GList *link_);
  71. void g_queue_delete_link (GQueue *queue,
  72.                                                          GList *link_);
3:实例
  1. #include <stdio.h>
  2. #include <glib.h>
  3. #include <glib/gprintf.h>

  4. struct map {
  5.     int key;
  6.     char *value;
  7. } m[10] = {
  8.     {0,"zero"},
  9.     {1,"one"},
  10.     {2,"two"},
  11.     {3,"three"},
  12.     {4,"four"},
  13.     {5,"five"},
  14.     {6,"six"},
  15.     {7,"seven"},
  16.     {8,"eight"},
  17.     {9,"nine"},
  18. };
  19. typedef struct map map;

  20. static void
  21. myPrintInt(gpointer p1, gpointer fmt)
  22. {
  23.     g_printf(fmt, *(gint *)p1);
  24. }

  25. static void
  26. myPrintStr(gpointer p1, gpointer fmt)
  27. {
  28.     g_printf(fmt, (gchar *)p1);
  29. }

  30. static void
  31. test_queue_1(void)
  32. {
  33. // GQueue* g_queue_new(void);
  34.     GQueue *queue = g_queue_new();
  35. // gboolean g_queue_is_empty(GQueue *queue);
  36.     gboolean b = g_queue_is_empty(queue);
  37.     g_printf("The queue should be empty now.\t\tResult: %s.\n", b ? "YES" : "NO");

  38. // void g_queue_push_tail(GQueue *queue, gpointer data);
  39.     gint i;
  40.     for (i = 0; i < sizeof (m) / sizeof (m[0]); i++)
  41.         g_queue_push_tail(queue, m[i].value);
  42.     
  43. // void g_queue_foreach(GQueue *queue, GFunc func, gpointer user_data);
  44.     g_printf("Now the queue[after push tail] :\n");
  45.     g_queue_foreach(queue, myPrintStr, "%s, ");
  46.     g_printf("\n");

  47. // guint g_queue_get_length(GQueue *queue);
  48.     g_printf("The lenght should be '%d' now.\t\tResult: %d.\n", 10, g_queue_get_length(queue));

  49. // void g_queue_reverse(GQueue *queue);
  50.     g_queue_reverse(queue);
  51.     g_printf("Now the queue[after reverse] :\n");
  52.     g_queue_foreach(queue, myPrintStr, "%s, ");
  53.     g_printf("\n");

  54. // gpointer g_queue_pop_head(GQueue *queue);
  55.     g_printf("The head should be '%s' now.\t\tResult: %s.\n", "nine", (gchar *)g_queue_pop_head(queue));

  56. // gpointer g_queue_pop_tail(GQueue *queue);
  57.     g_printf("The tail should be '%s' now.\t\tResult: %s.\n", "zero", (gchar *)g_queue_pop_tail(queue));
  58.     g_printf("Now the queue[after pop head and tail] :\n");
  59.     g_queue_foreach(queue, myPrintStr, "%s, ");
  60.     g_printf("\n");

  61. // gpointer g_queue_pop_nth(GQueue *queue, guint n);
  62.     g_printf("The head should be '%s' now.\t\tResult: %s.\n", "eight", (gchar *)g_queue_pop_nth(queue, 0));

  63. // void g_queue_push_head(GQueue *queue, gpointer data);
  64.     g_queue_push_head(queue, "zero");
  65.     g_queue_push_head(queue, "eight");

  66. // void g_queue_push_nth(GQueue *queue, gpointer data, gint n);
  67.     g_queue_push_nth(queue, "nine", 2);

  68.     g_printf("Now the queue[after push 'zero' 'eight' 'nine'] :\n");
  69.     g_queue_foreach(queue, myPrintStr, "%s, ");
  70.     g_printf("\n");

  71. // gpointer g_queue_peek_head(GQueue *queue);
  72.     g_printf("The head should be '%s' now.\t\tResult: %s.\n", "eight", (gchar *)g_queue_peek_head(queue));

  73. // gpointer g_queue_peek_tail(GQueue *queue);
  74.     g_printf("The tail should be '%s' now.\t\tResult: %s.\n", "zero", (gchar *)g_queue_peek_tail(queue));

  75. // gpointer g_queue_peek_nth(GQueue *queue, guint n);
  76.     g_printf("The head should be '%s' now.\t\tResult: %s.\n", "eight", (gchar *)g_queue_peek_nth(queue, 0));

  77. /*
  78. // void g_queue_clear(GQueue *queue);
  79.     g_queue_clear(queue);
  80.     g_printf("Now the queue[after clear] :\n");
  81.     g_queue_foreach(queue, myPrintStr, "%s, ");
  82.     g_printf("\n");
  83. */

  84. // void g_queue_free(GQueue *queue);
  85.     g_queue_free(queue);
  86. }

  87. static gint
  88. sort(gconstpointer p1, gconstpointer p2, gpointer user_data)
  89. {
  90.     gint32 a, b;
  91.     
  92.     a = *(gint*)(p1);
  93.     b = *(gint*)(p2);

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

  96. static gint
  97. myCompareInt(gconstpointer p1, gconstpointer p2)
  98. {
  99.     const int *a = p1;
  100.     const int *b = p2;

  101.     return *a - *b;
  102. }


  103. static gint
  104. sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data)
  105. {
  106.     gint32 a, b;
  107.     
  108.     a = *(gint*)(p1);
  109.     b = *(gint*)(p2);

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

  112. static void
  113. test_queue_2(void)
  114. {
  115.     GQueue *queue = NULL;
  116. /*
  117. // void g_queue_init(GQueue *queue);
  118.     g_queue_init(queue);
  119. */
  120.     queue = g_queue_new();

  121. // void g_queue_insert_sorted(GQueue *queue, gpointer data, GCompareDataFunc func gpointer user_data);
  122.     gint i;
  123.     for (i = 0; i < sizeof (m) / sizeof (m[0]); i++)
  124.         g_queue_insert_sorted(queue, &m[i].key, sort_r, NULL);

  125.     g_printf("Now the queue[after insert sorted] :\n");
  126.     for (i = 0; i < queue->length; i++)
  127.         g_printf("%d, ", *(gint *)g_queue_peek_nth(queue, i));
  128.     g_printf("\n");

  129. // void g_queue_remove(GQueue *queue, gconstpointer data);
  130.     g_queue_remove(queue, &m[3].key);
  131.     g_printf("Now the queue[after remove '%d'] :\n", m[3].key);
  132.     for (i = 0; i < queue->length; i++)
  133.         g_printf("%d, ", *(gint *)g_queue_peek_nth(queue, i));
  134.     g_printf("\n");

  135. // GList* g_queue_find_custom(GQueue *queue, gconstpointer data, GCompareFunc func);
  136. // void g_queue_insert_before(GQueue *queue, GList *sibling, gpointer data);
  137. // void g_queue_insert_after(GQueue *queue, GList *sibling, gpointer data);
  138.     g_queue_insert_before(queue, g_queue_find_custom(queue, &m[5].key, myCompareInt), &m[3].key);
  139.     g_queue_insert_after(queue, g_queue_find_custom(queue, &m[5].key, myCompareInt), &m[3].key);

  140.     g_printf("Now the queue[after insert '%d' around '%d'] :\n", m[3].key, m[5].key);
  141.     g_queue_foreach(queue, myPrintInt, "%d, ");
  142.     g_printf("\n");

  143. // void g_queue_sort(GQueue *queue, GCompareDataFunc compare, gpointer user_data);
  144.     g_queue_sort(queue, sort, NULL);

  145.     g_printf("Now the queue[sorted] :\n");
  146.     g_queue_foreach(queue, myPrintInt, "%d, ");
  147.     g_printf("\n");

  148. // GQueue* g_queue_copy(GQueue *queue);
  149.     GQueue *q = g_queue_copy(queue);
  150.     g_printf("Now the queue[copy] :\n");
  151.     g_queue_foreach(q, myPrintInt, "%d, ");
  152.     g_printf("\n");

  153. // void g_queue_remove_all(GQueue *queue, gconstpointer data);
  154.     g_queue_remove_all(q, &m[3].key);
  155.     g_printf("Now the queue[remove '%d'] :\n", m[3].key);
  156.     g_queue_foreach(q, myPrintInt, "%d, ");
  157.     g_printf("\n");

  158.     g_queue_free(q);
  159.     g_queue_free(queue);
  160. }

  161. int
  162. main(void)
  163. {
  164.     printf("BEGIN:\n************************************************************\n");
  165.     printf("\n------------------------------------------------------------\ntest_queue_1:\n");
  166.     test_queue_1();
  167.     printf("------------------------------------------------------------\n");
  168.     printf("\n------------------------------------------------------------\ntest_queue_2:\n");
  169.     test_queue_2();
  170.     printf("------------------------------------------------------------\n");
  171.     printf("\n************************************************************\nDONE\n");

  172.     return 0;
  173. }

4:结果

  1. [xied1@soho use]$ gcc `pkg-config --cflags --libs glib-2.0` -Wall -O2 -o queue queue.c
  2. [xied1@soho use]$ ./queue
  3. BEGIN:
  4. ************************************************************

  5. ------------------------------------------------------------
  6. test_queue_1:
  7. The queue should be empty now. Result: YES.
  8. Now the queue[after push tail] :
  9. zero, one, two, three, four, five, six, seven, eight, nine,
  10. The lenght should be '10' now. Result: 10.
  11. Now the queue[after reverse] :
  12. nine, eight, seven, six, five, four, three, two, one, zero,
  13. The head should be 'nine' now. Result: nine.
  14. The tail should be 'zero' now. Result: zero.
  15. Now the queue[after pop head and tail] :
  16. eight, seven, six, five, four, three, two, one,
  17. The head should be 'eight' now. Result: eight.
  18. Now the queue[after push 'zero' 'eight' 'nine'] :
  19. eight, zero, nine, seven, six, five, four, three, two, one,
  20. The head should be 'eight' now. Result: eight.
  21. The tail should be 'zero' now. Result: one.
  22. The head should be 'eight' now. Result: eight.
  23. ------------------------------------------------------------

  24. ------------------------------------------------------------
  25. test_queue_2:
  26. Now the queue[after insert sorted] :
  27. 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
  28. Now the queue[after remove '3'] :
  29. 9, 8, 7, 6, 5, 4, 2, 1, 0,
  30. Now the queue[after insert '3' around '5'] :
  31. 9, 8, 7, 6, 3, 5, 3, 4, 2, 1, 0,
  32. Now the queue[sorted] :
  33. 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9,
  34. Now the queue[copy] :
  35. 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9,
  36. Now the queue[remove '3'] :
  37. 0, 1, 2, 4, 5, 6, 7, 8, 9,
  38. ------------------------------------------------------------

  39. ************************************************************
  40. DONE

5:小结
  • 创建: g_queue_new()
  • 入队: g_queue_push_head() g_queue_push_tail()
  • 出队: g_queue_pop_head() g_queue_pop_tail()
  • 销毁: g_queue_free()



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