diff --git a/stack.c b/stack.c index 209089f62fb348a0d4779d667f6aba8bc2a0c1a1..fb5d80d2d9fe7f821cd9bfdaa60c783513826ae9 100644 --- a/stack.c +++ b/stack.c @@ -1,9 +1,6 @@ -#include <stdlib.h> -#include <stdbool.h> -#include <stdio.h> - #include "stack.h" - +#include <stdio.h> +#include <stdlib.h> #define DEFAULT_CAPACITY 4 @@ -87,4 +84,17 @@ int get_length(stack s) { bool stack_is_empty(stack s) { return s.top == -1; +} + +/** + * @brief Reset the stack's top to -1 + * New values will overwrite the old ones + * + * @param s + */ +void stack_clear(stack *s) +{ + s->top = -1; + s->capacity = DEFAULT_CAPACITY; + s->data = realloc(s->data, sizeof(int) * s->capacity); } \ No newline at end of file diff --git a/stack.h b/stack.h index 5aa60b4888579fba047ae8a656f61b86846b39bf..48803dd79aada27fb8a79b709687c74fae629c99 100644 --- a/stack.h +++ b/stack.h @@ -1,6 +1,10 @@ + #ifndef _STACK_H_ #define _STACK_H_ +#include <stdbool.h> + + typedef struct _stack { int *data; int capacity; @@ -17,6 +21,7 @@ void stack_peek(stack s, int *value); void stack_clone(stack s, stack *clone); int get_length(stack s); bool stack_is_empty(stack s); +void stack_clear(stack *s) ; void stack_print(const stack s);