Queue iterator
This commit is contained in:
1 parent
533366348d
commit
d603f3cc81
3 files changed
+282
-163
No files matched your search
+5
-5
@@ -80,9 +80,9 @@ int main(int argc, char const *argv[])
|
||||
sum1 += var;
|
||||
})
|
||||
|
||||
// FOR_EACH_REV(q, queue, , int, q, {
|
||||
// sum2 += var;
|
||||
// })
|
||||
FOR_EACH_REV(q, queue, , int, q, {
|
||||
sum2 += var;
|
||||
})
|
||||
|
||||
// FOR_EACH_REV(d, deque, , int, d, {
|
||||
// sum3 += var;
|
||||
@@ -96,8 +96,8 @@ int main(int argc, char const *argv[])
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
if (sum1 == 50005000)
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
// if (sum2 == 50005000)
|
||||
// printf("%10s PASSED\n", "QUEUE");
|
||||
if (sum2 == 50005000)
|
||||
printf("%10s PASSED\n", "QUEUE");
|
||||
// if (sum3 == 50005000)
|
||||
// printf("%10s PASSED\n", "DEQUE");
|
||||
if (sum4 == 50005000)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// Checks if the queue iterator can go back and forward in the queue without
|
||||
// hitting invalid states or a null pointer. Check is done visually.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../src/queue.h"
|
||||
|
||||
QUEUE_GENERATE(q, queue, static, , int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initliazie queue and add some elements
|
||||
queue *q = q_new(10);
|
||||
for (int i = 0; i < 10; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
// Initialize iterator
|
||||
queue_iter iter;
|
||||
q_iter(&iter, q);
|
||||
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (q_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n");
|
||||
while (q_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
for (q_iter_tostart(&iter); !q_iter_end(&iter); /**/)
|
||||
{
|
||||
q_iter_next(&iter, &r, &i);
|
||||
printf("Q[%2d] = %2d\n", i, r);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (q_iter_toend(&iter); !q_iter_start(&iter); /**/)
|
||||
{
|
||||
q_iter_prev(&iter, &r, &i);
|
||||
printf("Q[%2d] = %2d\n", i, r);
|
||||
}
|
||||
|
||||
q_free(q);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user