diff options
author | Marvin Borner | 2021-03-26 21:55:50 +0100 |
---|---|---|
committer | Marvin Borner | 2021-03-26 22:02:20 +0100 |
commit | 05498860e8f7b1e8bb27880bc7526de026694804 (patch) | |
tree | 3bddf16e9439a950a3810d45e42a5cefdbcb7663 /libc/stack.c | |
parent | a96e9c4c858d47f61b89d879aa0ce6a02bdacb38 (diff) |
Renamed libs
Cleaner and more flexible.
Diffstat (limited to 'libc/stack.c')
-rw-r--r-- | libc/stack.c | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/libc/stack.c b/libc/stack.c deleted file mode 100644 index 0cbb69d..0000000 --- a/libc/stack.c +++ /dev/null @@ -1,126 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#include <def.h> -#include <mem.h> -#include <stack.h> - -static int nonce = 0; - -struct stack *stack_new(void) -{ - struct stack *stack = malloc(sizeof(*stack)); - stack->tail = NULL; - return stack; -} - -void stack_destroy(struct stack *stack) -{ - struct stack_node *iterator = stack->tail; - while (iterator) { - if (!iterator->prev) { - free(iterator); - break; - } - iterator = iterator->prev; - free(iterator->next); - } - stack->tail = NULL; - free(stack); - stack = NULL; -} - -static struct stack_node *stack_new_node(void) -{ - struct stack_node *node = malloc(sizeof(*node)); - node->data = NULL; - node->prev = NULL; - node->next = NULL; - node->nonce = nonce++; - return node; -} - -static u32 stack_push_bot_node(struct stack *stack, struct stack_node *node) -{ - if (!stack || !node) - return 0; - - if (stack->tail) { - struct stack_node *iterator = stack->tail; - while (iterator) { - if (!iterator->prev) - break; - iterator = iterator->prev; - } - iterator->prev = node; - node->next = iterator; - } else { - stack->tail = node; - } - - return 1; -} - -static u32 stack_push_node(struct stack *stack, struct stack_node *node) -{ - if (!stack || !node) - return 0; - - if (stack->tail) { - stack->tail->next = node; - node->prev = stack->tail; - stack->tail = node; - } else { - stack->tail = node; - } - - return 1; -} - -u32 stack_empty(struct stack *stack) -{ - return !stack->tail; -} - -u32 stack_push_bot(struct stack *stack, void *data) -{ - struct stack_node *node = stack_new_node(); - node->data = data; - return stack_push_bot_node(stack, node); -} - -u32 stack_push(struct stack *stack, void *data) -{ - struct stack_node *node = stack_new_node(); - node->data = data; - return stack_push_node(stack, node); -} - -void *stack_pop(struct stack *stack) -{ - if (!stack || !stack->tail) - return NULL; - - struct stack_node *prev = stack->tail; - - if (stack->tail->prev) - stack->tail->prev->next = NULL; - stack->tail = stack->tail->prev; - - void *data = prev->data; - free(prev); - return data; -} - -void *stack_peek(struct stack *stack) -{ - if (!stack || !stack->tail) - return NULL; - - return stack->tail->data; -} - -void stack_clear(struct stack *stack) -{ - while (stack_pop(stack)) - ; -} |