mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
49 lines
889 B
C
49 lines
889 B
C
#include <stack.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
stack_t* stack_create() {
|
|
stack_t* retVal = malloc(sizeof(stack_t));
|
|
retVal->size = 0;
|
|
retVal->capacity = 10;
|
|
retVal->vals = malloc(retVal->capacity*sizeof(int));
|
|
return retVal;
|
|
}
|
|
|
|
void stack_free(stack_t* stack) {
|
|
free(stack->vals);
|
|
free(stack);
|
|
}
|
|
|
|
int stack_size(stack_t* stack) {
|
|
return stack->size;
|
|
}
|
|
|
|
void stack_push(stack_t* stack, int val) {
|
|
if(stack->size == stack->capacity) {
|
|
stack->capacity *= 2;
|
|
stack->vals = realloc(stack->vals, stack->capacity*sizeof(int));
|
|
}
|
|
stack->vals[stack->size++] = val;
|
|
}
|
|
|
|
int stack_pop(stack_t* stack) {
|
|
if (stack->size >= 1)
|
|
return stack->vals[--stack->size];
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
int stack_peak(stack_t* stack) {
|
|
if (stack->size >= 1)
|
|
return stack->vals[stack->size-1];
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
int stack_capacity(stack_t* stack) {
|
|
return stack->capacity;
|
|
}
|
|
|
|
|