Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "some_queue.h"
  3.  
  4. queue *new_queue(void) {
  5. queue *q = malloc(sizeof(queue));
  6. if (!q)
  7. return NULL;
  8.  
  9. q->elem_c = 0;
  10. q->curr_i = 0;
  11. q->q_head = NULL;
  12.  
  13. return q;
  14. }
  15.  
  16. void end_queue(queue *q) {
  17. if (q) {
  18. free(q->q_head);
  19. q->q_head = NULL;
  20. free(q);
  21. q = NULL;
  22. }
  23. }
  24.  
  25. void enqueue(queue *q, int type, void *val) {
  26. if (!q)
  27. return;
  28. q->q_head = realloc(q->q_head, (sizeof(item_t) * (q->elem_c + 1)));
  29. (q->q_head + q->elem_c)->type = type;
  30. (q->q_head + q->elem_c)->value = val;
  31. ++q->elem_c;
  32. }
  33.  
  34. item_t *dequeue(queue *q) {
  35. if (!q || q->elem_c == 0)
  36. return NULL;
  37. return ((q->q_head) + (q->curr_i++));
  38. }
  39.  
  40. item_t *queue_peek(queue *q) {
  41. if (!q || (q->elem_c == 0))
  42. return NULL;
  43. return (q->q_head + q->curr_i);
  44. }
  45.  
  46. uint32 queue_size(queue *q) {
  47. return (q) ? q->elem_c : (uint32)-1;
  48. }
  49.  
  50. int queue_next(queue *q) {
  51. if (!q || ((q->elem_c > 0) && (q->curr_i == q->elem_c - 1)))
  52. return 0;
  53. ++q->curr_i;
  54. return 1;
  55. }
  56.  
  57. int queue_back(queue *q) {
  58. if (!q || (q->curr_i == 0))
  59. return 0;
  60. --q->curr_i;
  61. return 1;
  62. }
  63.  
  64. int queue_first(queue *q) {
  65. if (!q)
  66. return 0;
  67. q->curr_i = 0;
  68. return 1;
  69. }
  70.  
  71. int queue_last(queue *q) {
  72. if (!q)
  73. return 0;
  74. q->curr_i = q->elem_c - 1;
  75. return 1;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement