Stack iterator
This commit is contained in:
1 parent
21c4fd836d
commit
533366348d
3 files changed
+127
-11
No files matched your search
+26
-4
@@ -59,27 +59,49 @@ int main(int argc, char const *argv[])
|
||||
|
||||
if (sum0 == 50005000)
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
if (sum4 == 50005000)
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
if (sum1 == 50005000)
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
if (sum2 == 50005000)
|
||||
printf("%10s PASSED\n", "QUEUE");
|
||||
if (sum3 == 50005000)
|
||||
printf("%10s PASSED\n", "DEQUE");
|
||||
if (sum4 == 50005000)
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
|
||||
|
||||
FOR_EACH_REV(l, list, , int, l, {
|
||||
sum0 += var;
|
||||
})
|
||||
|
||||
FOR_EACH_REV(s, stack, , int, s, {
|
||||
sum1 += var;
|
||||
})
|
||||
|
||||
// FOR_EACH_REV(q, queue, , int, q, {
|
||||
// sum2 += var;
|
||||
// })
|
||||
|
||||
// FOR_EACH_REV(d, deque, , int, d, {
|
||||
// sum3 += var;
|
||||
// })
|
||||
|
||||
FOR_EACH_REV(ll, linked, , int, ll, {
|
||||
sum4 += var;
|
||||
})
|
||||
|
||||
if (sum0 == 50005000)
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
if (sum1 == 50005000)
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
// if (sum2 == 50005000)
|
||||
// printf("%10s PASSED\n", "QUEUE");
|
||||
// if (sum3 == 50005000)
|
||||
// printf("%10s PASSED\n", "DEQUE");
|
||||
if (sum4 == 50005000)
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
else
|
||||
printf("%10s ERROR %d", "LINKEDLIST", sum4);
|
||||
|
||||
l_free(l);
|
||||
ll_free(ll);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Checks if the stack iterator can go back and forward in the stack without
|
||||
// hitting invalid states or a nul pointer. Check is done visually.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../src/stack.h"
|
||||
|
||||
STACK_GENERATE(s, stack, static, , int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initliazie stack and add some elements
|
||||
stack *s = s_new(10);
|
||||
for (int i = 0; i < 10; i++)
|
||||
s_push(s, i);
|
||||
|
||||
// Initialize iterator
|
||||
stack_iter iter;
|
||||
s_iter(&iter, s);
|
||||
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (s_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n");
|
||||
while (s_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
s_free(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user