Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
list.c

Single chained list example.

Single chained list example.

#include <banjo/assert.h>
#include <banjo/main.h>
#include <banjo/list.h>
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
// Create a new list
bj_list* list = bj_list_new_t(int); // No custom allocator
bj_assert(list); // Ensure list creation was successful
// Insert elements into the list
bj_list_insert(list, 0, &(int){10});
bj_list_insert(list, 1, &(int){20});
bj_list_insert(list, 2, &(int){30});
// Get the number of elements in the list
bj_assert(bj_list_len(list) == 3); // Ensure the count is correct
// Accessing elements by index
bj_assert(*(int*)bj_list_at(list, 0) == 10); // Ensure the value at index 0 is correct
bj_assert(*(int*)bj_list_at(list, 1) == 20); // Ensure the value at index 1 is correct
bj_assert(*(int*)bj_list_at(list, 2) == 30); // Ensure the value at index 2 is correct
// Iterating over the list using an iterator
bj_assert(iterator); // Ensure iterator creation was successful
while (bj_list_iterator_has_next(iterator)) {
}
// Clearing the list
bj_assert(bj_list_len(list) == 0); // Ensure the list is cleared
// Deleting the list
bj_list_del(list);
return 0;
}
void bj_list_clear(bj_list *list)
Clears all data in the list.
void * bj_list_at(bj_list *list, size_t index)
Returns the value of the element at the specified index.
struct bj_list_t bj_list
Typedef for the BList_t struct.
Definition list.h:23
void * bj_list_insert(bj_list *list, size_t index, void *p_data)
Inserts a new element at the specified index and returns a pointer to it.
void bj_list_del(bj_list *p_list)
Deletes a bj_list object and releases associated memory.
#define bj_list_new_t(T)
Creates a new bj_list with type inference for elements.
Definition list.h:54
size_t bj_list_len(bj_list *list)
Returns the number of elements in the given list.
bj_list_iterator * bj_list_iterator_new(bj_list *list)
Creates a forward-only iterator for the given list.
void * bj_list_iterator_next(bj_list_iterator *iterator)
Returns the next element in the iteration.
struct bj_list_iterator_t bj_list_iterator
Typedef for the bj_list_iterator_t structure.
Definition list.h:184
bj_bool bj_list_iterator_has_next(bj_list_iterator *iterator)
Returns BJ_TRUE if the iterator has more elements.
void bj_list_iterator_del(bj_list_iterator *iterator)
Destroys the given list iterator.
Header file for List container type.