From 712ff65af4e00ebf7703fa6e4be9170a3849db8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 26 Sep 2018 18:22:36 +0200 Subject: [PATCH] added and fixed stack --- method_dispatcher/test/Makefile | 2 +- stack.c | 20 +++++++++++++++++--- stack.h | 6 ++++++ test/Makefile | 12 ++++++++++++ test/test_stack.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 test/Makefile create mode 100644 test/test_stack.c diff --git a/method_dispatcher/test/Makefile b/method_dispatcher/test/Makefile index 7997126..c9fc7c9 100644 --- a/method_dispatcher/test/Makefile +++ b/method_dispatcher/test/Makefile @@ -3,7 +3,7 @@ CC=gcc all: test_method_dispatcher method_dispatcher: clean - $(CC) ../method_dispatcher.c test_method_dispatcher.c -o method_dispatcher + $(CC) -g ../method_dispatcher.c test_method_dispatcher.c -o method_dispatcher test_method_dispatcher: method_dispatcher valgrind ./method_dispatcher diff --git a/stack.c b/stack.c index 706ae7d..97d4a8c 100644 --- a/stack.c +++ b/stack.c @@ -10,17 +10,31 @@ char bci_stack_t_push(bci_stack_t * stack, uint16_t value) } node->next = *stack; node->value = value; - stack = &node; + *stack = node; return 0; } char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result) { + bci_stack_t this_node; if(!*stack) { return 1; } - *result = (*stack)->value; - *stack = (*stack)->next; + this_node = *stack; + *result = this_node->value; + *stack = this_node->next; + free(this_node); return 0; } +void bci_stack_t_del(bci_stack_t * stack) +{ + bci_stack_t this_node; + + while(*stack) + { + this_node = *stack; + *stack = this_node->next; + free(this_node); + } +} diff --git a/stack.h b/stack.h index 894790e..0522db0 100644 --- a/stack.h +++ b/stack.h @@ -11,4 +11,10 @@ typedef struct bci_stack_s char bci_stack_t_push(bci_stack_t * stack, uint16_t value); char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result); + +/* + * Deletes the entire (!) stack. + * + * */ +void bci_stack_t_del(bci_stack_t * stack); #endif diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..a90518d --- /dev/null +++ b/test/Makefile @@ -0,0 +1,12 @@ +CC=gcc + +all: test_stack + +stack: clean + $(CC) -g ../stack.c test_stack.c -o stack + +test_stack: stack + valgrind ./stack + +clean: + -rm stack diff --git a/test/test_stack.c b/test/test_stack.c new file mode 100644 index 0000000..05fecc1 --- /dev/null +++ b/test/test_stack.c @@ -0,0 +1,29 @@ +#include +#include "../stack.h" + +int main(void) +{ + bci_stack_t stack = NULL; + int i; + uint16_t res; + char status = 0; + + + for(i = 0; i < 10; i++) + { + bci_stack_t_push(&stack, (uint16_t) i); + } + while(!status) + { + status = bci_stack_t_pop(&stack, &res); + printf("%d\n", res); + } + + + for(i = 0; i < 10; i++) + { + bci_stack_t_push(&stack, (uint16_t) i); + } + bci_stack_t_del(&stack); + return 0; +}