gqueue.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * gqueue.c: Queue
  3. *
  4. * Author:
  5. * Duncan Mak ([email protected])
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * (C) 2006 Novell, Inc.
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <glib.h>
  31. gpointer
  32. g_queue_pop_head (GQueue *queue)
  33. {
  34. gpointer head = NULL;
  35. if (queue->length != 0){
  36. GList *old_head;
  37. old_head = queue->head;
  38. head = old_head->data;
  39. queue->head = old_head->next;
  40. queue->length--;
  41. g_list_free_1 (old_head);
  42. }
  43. return head;
  44. }
  45. gboolean
  46. g_queue_is_empty (GQueue *queue)
  47. {
  48. if (!queue)
  49. return TRUE;
  50. return queue->length == 0;
  51. }
  52. void
  53. g_queue_push_head (GQueue *queue, gpointer head)
  54. {
  55. if (!queue)
  56. return;
  57. queue->head = g_list_prepend (queue->head, head);
  58. if (!queue->tail)
  59. queue->tail = queue->head;
  60. queue->length ++;
  61. }
  62. GQueue *
  63. g_queue_new (void)
  64. {
  65. GQueue *queue = g_new0 (GQueue, 1);
  66. queue->length = 0;
  67. return queue;
  68. }
  69. void
  70. g_queue_free (GQueue *queue)
  71. {
  72. if (!queue)
  73. return;
  74. g_list_free (queue->head);
  75. g_free (queue);
  76. }