diff --git a/stack/include/stack.h b/stack/include/stack.h index d27e992f64dabca2b253e70c109e128fcaf1e5c5..92c9855c4b34e9af53c5df2f74960dbc3842944a 100644 --- a/stack/include/stack.h +++ b/stack/include/stack.h @@ -10,22 +10,21 @@ #include <stdint.h> #include <stdlib.h> - - #ifndef _JSTACK_H_ #define _JSTACK_H_ // DEFINES -#define MAX_CAPACITY 500 +#define INCREMENT_STACK 100 typedef struct { + uint64_t max; int64_t top; int32_t *data; }stack_t; // PROTOTYPE FUNCTIONS -int32_t* create_stack(); +int32_t* create_stack(uint64_t capa); void stack_push(stack_t* st, int32_t val); int32_t stack_pop(stack_t* st); int32_t stack_peek(stack_t st); diff --git a/stack/src/stack.c b/stack/src/stack.c index b5bfae57a53c5c73bb6a1f87e55cec3a255ad640..069ca2a7e5d3cc33dceddff88ff11e4fa975d0b2 100644 --- a/stack/src/stack.c +++ b/stack/src/stack.c @@ -7,21 +7,26 @@ #include "../include/stack.h" -int32_t* create_stack() +int32_t* create_stack(uint64_t capa) { stack_t* st = malloc(sizeof(stack_t)); - st->data = malloc(MAX_CAPACITY * sizeof(int32_t)); + st->max = capa; + st->data = malloc(st->max * sizeof(int32_t)); st->top = -1; } void stack_push(stack_t* st, int32_t val) { + // UP CAPACITY WHEN MAX IS REACHED + if(st->top == st->max) + { + realloc(st->data, st->max + INCREMENT_STACK); + } st->data[++st->top] = val; } int32_t stack_pop(stack_t* st) { - return st->data[st->top--]; } @@ -32,7 +37,7 @@ int32_t stack_peek(stack_t st) bool stack_is_full(stack_t st) { - return st.top == MAX_CAPACITY; + return st.top == st.max; } bool stack_is_empty(stack_t st)