A lightweight generic queue implementation in C. This library provides a complete FIFO (First-In-First-Out) queue data structure that works with any data type through void* pointer.
Library: Pure C Tests: C++ with Google Test
- Generic implementation supporting any data type
- Deep copy semantics for safe memory management
- Search functionality with custom predicates or memory comparison
- Queue reversal and copy operations
- Comprehensive error handling
- Zero dependencies for library users
mkdir build && cd build
cmake ..
makemkdir build && cd build
cmake ..
cmake --build .On Windows, executables and libraries will be in the Debug/ or Release/ folder. You can also open the generated .sln file in Visual Studio.
./exampleDebug\example.exeThe library includes comprehensive unit tests written in C++ using Google Test.
cd build
ctest
# Or run the test executable
./tests/QueueTest
# Or run the test target (after make all)
make testcd build
ctest
# or
Debug\QueueTest.exeFor linux
mkdir build && cd build
cmake ..
make
sudo make installThis installs:
libgenericQueue.ato/usr/local/libqueue.hto/usr/local/include
To install to a custom location:
cmake .. -DCMAKE_INSTALL_PREFIX=/custom/path
make
make installYou can also install on windows using the cmake commands
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=C:<YOUR_PATH>
cmake --build . --config Release
cmake --install . --config ReleaseYou need to configure then your project or cmake file to link using that location
queue *createQueue(size_t allocSize)- Create a new queuevoid destroyQueue(queue **q)- Destroy queue and free memoryqueue *enqueue(queue *q, void *data)- Add element to backqueue *dequeue(queue *q, void *data)- Remove and retrieve front elementqueue *front(queue *q, void *data)- Peek at front elementqueue *reverse(queue *q)- Reverse the queue orderqueue *clearQueue(queue *q)- Remove all elementsqueue *copyQueue(queue *src)- Create a deep copyvoid *find(queue *q, bool (*predicate)(void *data))- Find element with predicatevoid *findMem(queue *q, void *data)- Find element by memory comparisonsize_t getSize(queue *q)- Get number of elementsbool isEmpty(queue *q)- Check if queue is emptysize_t getAllocationSize(queue *q)- Get element size
See queue.h for detailed documentation.
#include "queue.h"
#include <stdio.h>
typedef struct {
int id;
char name[32];
} Person;
int main() {
// Create queue for Person structs
queue *q = createQueue(sizeof(Person));
if (q == NULL) {
fprintf(stderr, "Failed to create queue\n");
return 1;
}
// Add elements
Person p1 = {1, "Alice"};
Person p2 = {2, "Bob"};
enqueue(q, &p1);
enqueue(q, &p2);
printf("Queue size: %zu\n", getSize(q));
// Peek at front
Person front_person;
front(q, &front_person);
printf("Front: %s\n", front_person.name);
// Remove elements
Person dequeued;
while (!isEmpty(q)) {
dequeue(q, &dequeued);
printf("Dequeued: %s\n", dequeued.name);
}
// Clean up
destroyQueue(&q);
return 0;
}