libcdsb/examples/list.c

52 lines
1.1 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdio.h>
#include <stdlib.h>
#include "../include/list.h"
typedef vtype_list list_t;
int print_value(void* value, ssize_t index, vtype type, void* data) {
const char *n = data;
switch (type) {
default: abort();
case VTYPE_INT32:
printf("%s %d (index: %ld)\n", n, *(vtype_int32*)value, index);
break;
case VTYPE_FLOAT:
printf("%s %f (index: %ld)\n", n, *(vtype_float*)value, index);
break;
}
return 0;
}
int main(int argc, char** argv) {
list_t list;
vtype_float fl = 0.0;
list_init(&list);
for (vtype_int32 i = 0; i < 28; ++i) {
if (i%2) {
list_push_back(&list, i);
} else {
list_push_back(&list, fl);
fl += 0.05;
}
}
list_get_by_index(&list, 13, "Get value:", print_value);
list_pop_by_index(&list, 18, "Pop value:", print_value);
list_foreach(&list, "Foreach loop:", print_value);
list_free(&list);
}