]> git.datanom.net - clib.git/commitdiff
Initial commit
authorMichael Rasmussen <mir@datanom.net>
Sat, 1 Jun 2024 00:18:55 +0000 (02:18 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sat, 1 Jun 2024 00:18:55 +0000 (02:18 +0200)
Signed-off-by: Michael Rasmussen <mir@datanom.net>
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
demo.c [new file with mode: 0644]
queue.c [new file with mode: 0644]
queue.h [new file with mode: 0644]
stack.c [new file with mode: 0644]
stack.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..f5a4a63
--- /dev/null
@@ -0,0 +1,4 @@
+*.o
+*.geany
+demo
+
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
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 (file)
index 0000000..b8b5370
--- /dev/null
+++ b/demo.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdlib.h>
+#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 (file)
index 0000000..d2112ad
--- /dev/null
+++ b/queue.c
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#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 (file)
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 (file)
index 0000000..bff5f9b
--- /dev/null
+++ b/stack.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+#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 (file)
index 0000000..2ff3a7d
--- /dev/null
+++ b/stack.h
@@ -0,0 +1,15 @@
+#ifndef _STACK_H_
+#define _STACK_H_
+
+#include <stdbool.h>
+
+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
This page took 0.039044 seconds and 5 git commands to generate.