Select Git revision

ines.maya authored
stack.c 626 B
#include "stack.h"
void stack_init(stack *s){
s->top = -1;
s->capacity = DEFAULT_CAPACITY;
s->data = malloc(sizeof(int) * DEFAULT_CAPACITY);
}
bool stack_is_empty(stack s){
return s.top == -1;
}
void stack_peek(stack s, int *value){
if (!stack_is_empty(s)) {
*value = s.data[s.top];
}
printf("peek : value %d, top : %d\n", s.data[s.top],s.top);
}
// depile
void stack_pop(stack *s, int *value){
if (stack_is_empty(*s)){
return;
}
if (s->top == s->capacity/4){
s->capacity /= 2;
}
*value = s->data[s->top];
printf("pop : %d, top : %d\n", s->data[s->top],s->top);
s->top -= 1;
}