
A generic, portable C data structures library with support for multiple underlying implementations. Designed to work across C89, C99, C11 and C23 standards.
Features
- Portable — supports C89 through C23 with automatic standard detection
- Generic — all data structures work with any data type via
void * and memcpy
- Multiple implementations — choose between array based and linked list backends at runtime
- Safe API — every function returns a
tr_result_t error code, enforced by TR_NODISCARD
- Cross platform — tested on Ubuntu and Windows with GCC and Clang
- Well documented — full API documentation available at GitHub Pages
Data Structures
| Structure | Array Dynamic | Array Fixed | Linked List | Status |
| Stack | ✅ | ✅ | ✅ | Completed |
| Queue | ⬜ | ⬜ | ⬜ | Planned |
Requirements
- CMake 3.21 or later
- C compiler — GCC, Clang, or MSVC
- Ninja (recommended) or Unix Makefiles
Building
Clone the repository
git clone https://github.com/TiagoRodrigues1111/TR_datastructures.git
cd TR_datastructures
Configure and build
Using CMake presets (recommended):
# debug build
cmake --preset debug
cmake --build --preset debug
# release build
cmake --preset release
cmake --build --preset release
Using CMake directly:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
Build options
| Option | Default | Description |
TR_BUILD_TESTS | OFF | Build the test suite |
TR_BUILD_EXAMPLES | OFF | Build the usage examples |
TR_BUILD_BENCHMARKS | OFF | Build the benchmarks |
TR_BUILD_FUZZ | OFF | Build the fuzz targets |
Example with options:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DTR_BUILD_TESTS=ON -DTR_BUILD_EXAMPLES=ON
cmake --build build
Running tests
ctest --test-dir build --output-on-failure
Usage
Including the library
Include the umbrella header to get access to the full API:
Or include individual headers as needed:
Public API for the stack data structure.
Stack example
int main(void)
{
struct stack *p_stack =
NULL;
int val = 42;
int top = 0;
{
return 1;
}
{
return 1;
}
{
printf("Top value: %d\n", top);
}
return 0;
}
#define NULL
Null pointer constant.
Definition tr_types.h:199
@ TR_OK
Definition tr_result.h:71
enum tr_result tr_result_t
Library wide error code returned by all API functions.
tr_result_t tr_stack_create(size_t size_of_datatype, size_t elements_to_allocate, tr_stack_type_t stack_type, struct stack **id_of_stack)
Allocates and initialises a new stack instance.
@ TR_STACK_ARRAY_DYNAMIC
Definition tr_stack.h:127
tr_result_t tr_stack_pop(struct stack *id_of_stack)
Removes the element at the top of the stack.
tr_result_t tr_stack_push(struct stack *id_of_stack, const void *data_to_push)
Pushes a deep copy of the data onto the top of the stack.
tr_result_t tr_stack_destroy(struct stack **id_of_stack)
Frees all memory associated with the stack instance.
tr_result_t tr_stack_top(const struct stack *id_of_stack, void *data_at_top)
Copies the element at the top of the stack into the provided buffer.
Stack implementation types
@ TR_STACK_LL
Definition tr_stack.h:129
@ TR_STACK_ARRAY_FIXED
Definition tr_stack.h:128
Error handling
Every API function returns a tr_result_t:
{
tr_result
Library wide error code returned by all API functions.
Definition tr_result.h:70
@ TR_ERR_OUT_OF_RANGE
Definition tr_result.h:79
@ TR_ERR_ALLOC
Definition tr_result.h:73
@ TR_ERR_DUPLICATE
Definition tr_result.h:78
@ TR_ERR_INVALID
Definition tr_result.h:76
@ TR_ERR_EMPTY
Definition tr_result.h:74
@ TR_ERR_FULL
Definition tr_result.h:75
@ TR_ERR_NOT_FOUND
Definition tr_result.h:77
@ TR_ERR_NULL
Definition tr_result.h:72
The TR_NODISCARD attribute causes a compiler warning if a return value is ignored, encouraging proper error handling.
Version checking
#if TR_VERSION_AT_LEAST(1, 0, 0)
#endif
#define TR_VERSION_STRING
Definition tr_version.h:85
Version information for the tr_datastructures library.
Documentation
Full API documentation is available at:
https://tiagorodrigues1111.github.io/TR_datastructures/
To generate documentation locally:
# Windows
scripts\generate_docs.bat
# Linux / macOS
./scripts/generate_docs.sh
Documentation is generated into docs/output/html/index.html.
Project structure
TR_datastructures/
├── include/
│ ├── tr_datastructures.h # umbrella header
│ └── tr_datastructures/
│ ├── tr_export.h # symbol visibility macros
│ ├── tr_result.h # error code enum
│ ├── tr_types.h # portable type definitions
│ ├── tr_version.h # version information (generated)
│ └── tr_stack.h # stack public API
├── src/
│ ├── internal/
│ │ └── include/
│ │ └── tr_internal.h # internal utility macros
│ ├── stack/
│ │ ├── stack.c # array based implementation
│ │ └── stack_ll.c # linked list implementation (planned)
│ └── tr_datastructures.c # library placeholder
├── tests/
│ └── stack/
│ └── test_stack.c # stack unit tests
├── examples/
│ └── stack/
│ └── example_stack.c # stack usage examples
├── benchmarks/ # benchmarks (planned)
├── fuzz/ # fuzz targets (planned)
├── docs/
│ └── Doxyfile # Doxygen configuration
├── scripts/
│ ├── generate_docs.sh # documentation generation (Linux)
│ └── generate_docs.bat # documentation generation (Windows)
├── cmake/
│ ├── tr_datastructures-config.cmake.in
│ └── tr_version.h.in
├── .github/
│ └── workflows/
│ ├── build_test.yml # CI build and test
│ └── docs.yml # documentation deployment
├── CMakeLists.txt
├── CMakePresets.json
└── vcpkg.json
Contributing
Contributions are welcome. Please read CONTRIBUTING.md before submitting a pull request.
License
This project is released into the public domain under the [Unlicense](LICENSE).