List iterator

This commit is contained in:
Leonardo committed 2019-03-22 22:13:37 -03:00
1 parent 945fd71c12
commit 21c4fd836d
4 files changed
+113 -19

No files matched your search

+4 -4
View File
@@ -4,9 +4,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "../../src/macro_collections.h"
#include "../../src/linkedlist.h"
CONTAINER_GENERATE(LINKEDLIST, PUBLIC, ll, linked, static, , int)
LINKEDLIST_GENERATE(ll, linked, static, , int)
int main(int argc, char const *argv[])
{
@@ -26,10 +26,10 @@ int main(int argc, char const *argv[])
for (int j = 0; j < 4; j++)
{
while (ll_iter_next(&iter, &r, &i))
printf("list[%2d] = %2d\n", i, r);
printf("C[%2d] = %2d\n", i, r);
printf("\n");
while (ll_iter_prev(&iter, &r, &i))
printf("list[%2d] = %2d\n", i, r);
printf("C[%2d] = %2d\n", i, r);
printf("\n\n");
}
+39
View File
@@ -0,0 +1,39 @@
// Checks if the list iterator can go back and forward in the list 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/list.h"
LIST_GENERATE(l, list, static, , int)
int main(int argc, char const *argv[])
{
size_t i;
int r;
// Initliazie list and add some elements
list *l = l_new(10);
for (int i = 0; i < 10; i++)
l_push_back(l, i);
// Initialize iterator
list_iter iter;
l_iter(&iter, l);
// Make the iterator go back and forward
for (int j = 0; j < 4; j++)
{
while (l_iter_next(&iter, &r, &i))
printf("C[%2d] = %2d\n", i, r);
printf("\n");
while (l_iter_prev(&iter, &r, &i))
printf("C[%2d] = %2d\n", i, r);
printf("\n\n");
}
l_free(l);
return 0;
}