Guest User

Untitled

a guest
Jun 1st, 2019
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "queue.h"
  2.  
  3. // Functions
  4.  
  5. void queue_initialize(queue *q, data_string to_string, data_destroy destroy) {
  6.  
  7. q->capacity = QUEUE_SIZE;
  8. q->values = malloc(sizeof(data) * q->capacity);
  9. assert(q->values != NULL);
  10. q->front = 0;
  11. q->rear = 0;
  12. q->size = 0;
  13. q->to_string = to_string;
  14. q->destroy = destroy;
  15. return;
  16. }
  17.  
  18. void queue_destroy(queue *q) {
  19. int i;
  20. for (i = 0; i < q->size; i++) {
  21. q->destroy(&q->values[i]);
  22. }
  23. free(q->values);
  24. q->values = NULL;
  25. q->capacity = 0;
  26. q->front = 0;
  27. q->rear = 0;
  28. q->size = 0;
  29. return;
  30. }
  31.  
  32. int queue_empty(const queue *q) {
  33.  
  34. return q->size == 0;
  35.  
  36. }
  37.  
  38. int queue_full(const queue *q) {
  39. // Assume queue is not full.
  40. assert(q->size < q->capacity);
  41. return 0;
  42. }
  43.  
  44. void queue_insert(queue *q, data v) {
  45. q->rear = (q->rear + 1) % q->capacity;
  46. if (q->size == q->rear) {
  47. q->capacity <<= 1;
  48. q->values = realloc(q->values, sizeof(data) * q->capacity);
  49. }
  50. q->values[q->rear] = v;
  51. q->size++;
  52. return;
  53. }
  54.  
  55. data queue_peek(const queue *q) {
  56. // Cannot peek on an empty queue.
  57. assert(q->front != -1);
  58. return q->values[q->front];
  59.  
  60. }
  61.  
  62. data queue_remove(queue *q) {
  63. // Cannot remove on an empty queue.
  64. assert(q->front != -1);
  65. data v = q->values[q->front];
  66. q->front = (q->front + 1) % q->capacity;
  67. q->size--;
  68. return v;
  69.  
  70. }
  71.  
  72. void queue_print(const queue *q) {
  73. // Iterate through the queue.
  74. char string[MAX_LINE];
  75. int j = q->front;
  76.  
  77. for (int i = 0; i < q->size; i++) {
  78. printf("%s\n", q->to_string(string, MAX_LINE, &(q->values[j])));
  79. j = (j + 1) % q->capacity;
  80. }
  81. printf("\n");
  82. return;
  83. }
  84.  
  85. void queue_combine(queue *target, queue *source1, queue *source2) {
  86.  
  87. while (source1->size > 0 && source2->size > 0) {
  88. queue_insert(target, queue_remove(source1));
  89. queue_insert(target, queue_remove(source2));
  90. }
  91. while (source1->size > 0) {
  92. queue_insert(target, queue_remove(source1));
  93. }
  94. while (source2->size > 0) {
  95. queue_insert(target, queue_remove(source2));
  96. }
  97. return;
  98. }
  99.  
  100. void queue_split(queue *target1, queue *target2, queue *source) {
  101. int i = 0;
  102. while (source->size > 0) {
  103. if (i % 2 == 0) {
  104. queue_insert(target1, queue_remove(source));
  105. } else {
  106. queue_insert(target2, queue_remove(source));
  107. }
  108. i++;
  109. }
  110. return;
  111. }
  112.  
  113. int queue_identical(queue *q1, queue *q2) {
  114. int identical = 0;
  115. int i = 0;
  116. if (q1->size != q2->size) {
  117. identical = 1;
  118. } else {
  119. while (identical == 0 && i < q2->size) {
  120. if (movie_compare(&q1->values[i], &q2->values[i]) != 0) {
  121. identical = 1;
  122. } else {
  123. i++;
  124. }
  125. }
  126. }
  127. return identical;
  128. }
RAW Paste Data