From: Michael Rasmussen Date: Sat, 1 Jun 2024 00:18:55 +0000 (+0200) Subject: Initial commit X-Git-Url: http://git.datanom.net/clib.git/commitdiff_plain/d104c68717e3cfa17762fd8e10c40d80768895fc Initial commit Signed-off-by: Michael Rasmussen --- d104c68717e3cfa17762fd8e10c40d80768895fc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5a4a63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.geany +demo + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..87f260f --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -Wall -g +DEPS = queue.h stack.h + +%.o: %.c $(DEPS) + $(CC) $< $(CFLAGS) -c -o $@ + +demo: demo.o queue.o stack.o + $(CC) $(CFLAGS) -o demo demo.o queue.o stack.o + +clean: + rm -rf *.dSYM *.so *.a *.o demo + diff --git a/demo.c b/demo.c new file mode 100644 index 0000000..b8b5370 --- /dev/null +++ b/demo.c @@ -0,0 +1,27 @@ +#include +#include +#include "queue.h" +#include "stack.h" + +int main() { + Stack* stack = stack_init(); + if (stack_empty(stack)) + printf("Stack is empty\n"); + + int* i = malloc(sizeof(int*)); + *i = 10; + stack_push(stack, i); + int* j = malloc(sizeof(int*)); + *j = 5; + stack_push(stack, j); + int* val = stack_pop(stack); + printf("Stack back: %i\n", *val); + free(val); + void** elems = stack_clear(&stack); + for (int i = 0; elems[i]; i++) + free(elems[i]); + free(elems); + + return 0; +} + diff --git a/queue.c b/queue.c new file mode 100644 index 0000000..d2112ad --- /dev/null +++ b/queue.c @@ -0,0 +1,20 @@ +#include +#include "queue.h" + +void* queue_pop() { + return NULL; +} + +void queue_push(void*) { +} + +void* queue_front() { + return NULL; +} + +void* queue_back() { + return NULL; +} + +void queue_clear() { +} diff --git a/queue.h b/queue.h new file mode 100644 index 0000000..6414481 --- /dev/null +++ b/queue.h @@ -0,0 +1,11 @@ + +#ifndef _QUEUE_H_ +#define _QUEUE_H_ + +void* queue_pop(); +void queue_push(void*); +void* queue_front(); +void* queue_back(); +void queue_clear(); + +#endif diff --git a/stack.c b/stack.c new file mode 100644 index 0000000..bff5f9b --- /dev/null +++ b/stack.c @@ -0,0 +1,53 @@ +#include +#include "stack.h" + +typedef struct _Stack { + void** elems; + int size; +} Stack; + +Stack* stack_init() { + Stack* s = malloc(sizeof(Stack)); + s->size = 0; + s->elems = NULL; + return s; +} + +void* stack_pop(Stack* stack) { + if (stack_empty(stack)) + return NULL; + stack->size -= 1; + void* elem = stack->elems[stack->size]; + stack->elems[stack->size] = NULL; + stack->elems = realloc(stack->elems, stack->size * sizeof(void*)); + + return elem; +} + +void stack_push(Stack* stack, void* elem) { + if (stack == NULL) + return; + + stack->size += 1; + stack->elems = realloc(stack->elems, stack->size * sizeof(void*)); + stack->elems[stack->size - 1] = elem; +} + +void* stack_peek(Stack* stack) { + if (stack_empty(stack)) + return NULL; + return stack->elems[stack->size - 1]; +} + +bool stack_empty(Stack* stack) { + return (stack == NULL || stack->size == 0); +} + +void** stack_clear(Stack** stack) { + if (*stack == NULL) + return NULL; + void** elems = (*stack)->elems; + free(*stack); + *stack = NULL; + return elems; +} diff --git a/stack.h b/stack.h new file mode 100644 index 0000000..2ff3a7d --- /dev/null +++ b/stack.h @@ -0,0 +1,15 @@ +#ifndef _STACK_H_ +#define _STACK_H_ + +#include + +typedef struct _Stack Stack; + +Stack* stack_init(); +void* stack_pop(Stack*); +void stack_push(Stack*, void*); +void* stack_peek(Stack*); +bool stack_empty(Stack*); +void** stack_clear(Stack** stack); + +#endif