Commit 9584ab55 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Realloc in push

parent 1b8ce2a5
......@@ -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);
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment