From 64feef1b9ea72adf7ba32998e9dca7d507607498 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Mon, 2 Jan 2023 22:31:49 -0800 Subject: a lot of stuff. --- src/stack.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/stack.c (limited to 'src/stack.c') diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..4898d83 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,34 @@ +#include "./include/stack.h" +#include "./include/hash_table.h" +#include "./include/macros.h" +#include + +stack_t *init_stack(int ht_size) { + stack_t *s = (stack_t *)malloc(sizeof(stack_t)); + if (s == NULL) + die("malloc on stack"); + s->stack = NULL; + s->cur = -1; + return s; +} + +void stack_push(stack_t *s, hash_table_t *h) { + if (s->stack == NULL) { + s->stack = malloc(sizeof(hash_table_t *)); + if (s->stack == NULL) + die("malloc on stack within stack"); + } else { + s->stack = realloc(s->stack, 2 + s->cur); + } + s->cur++; + s->stack[s->cur] = h; +} + +hash_table_t *stack_peek(stack_t *s) { return s->stack[s->cur]; } + +hash_table_t *stack_pop(stack_t *s) { + hash_table_t *h = s->stack[s->cur]; + s->stack[s->cur] = NULL; + s->cur--; + return h; +} -- cgit