1
0
Fork
You've already forked list.h
0
A header-only library which can implement a list over any type.
  • C 100%
2026年05月15日 03:41:32 +01:00
LICENSE Initial commit 2026年05月15日 02:28:40 +01:00
list.h Document return value for List_push 2026年05月15日 03:41:32 +01:00
README.md Fix typo 2026年05月15日 02:33:24 +01:00

list.h

A header-only library which can implement a list over any type.

Example

#define LIST_IMPL
#include "list.h"
// Refer to list.h for other constants you can change

/**
 * All list structs must at least have these 3 fields.
 * data: Where the data will be stored, in this case lots of ints.
 * capacity: Stores the allocated capacity of the list (the number of elements the list can store).
 * count: The number of elements currently stored in the list.
 */
typedef struct {
 int *data;
 size_t capacity;
 size_t count;
} IntList;
typedef struct {
 char **data;
 size_t capacity;
 size_t count;
} StrList;
int main(void) {
 int result = 0;
 // Initializing everything to 0 is the default state
 // You *must* do this before using a list
 IntList ints = {0};
 StrList strs = {0};
 // Provided the lists are zeroed, you can use them straight away
 // No need to call any `init` functions
 for (int i = 0; i < 1024; i++) {
 if (List_push(&ints, i) == false) {
 result = 1;
 goto end;
 }
 }
 for (int i = 0; i < 16; i++) {
 if (List_push(&strs, "Test") == false) {
 result = 1;
 goto end;
 }
 }
 puts("==== Ints ====");
 while (List_empty(ints) == false) {
 printf("%d ", List_pop(&ints));
 }
 puts("");
 puts("==== Strs ====");
 while (List_empty(strs) == false) {
 printf("%s ", List_pop(&strs));
 }
 puts("");
end:
 // Make sure you clean up the lists at the end
 List_free(&ints);
 List_free(&strs);
 return result;
}