2013-09-19 21:51:49 +00:00
|
|
|
#include "stack.h"
|
|
|
|
#include "../lib/xalloc.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
struct stack {
|
|
|
|
size_t max_size;
|
|
|
|
size_t cur_size;
|
|
|
|
void** arr;
|
|
|
|
};
|
|
|
|
|
|
|
|
stack_t* alloc_stack(size_t size)
|
|
|
|
{
|
|
|
|
stack_t* stack = xmalloc(sizeof(stack_t));
|
|
|
|
stack->arr = xcalloc(size, sizeof(void*));
|
|
|
|
stack->max_size = size;
|
|
|
|
stack->cur_size = 0;
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_stack(stack_t* stack)
|
|
|
|
{
|
|
|
|
xfree(stack->arr);
|
|
|
|
xfree(stack);
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:10:20 +00:00
|
|
|
void push(stack_t* stack, void* elt)
|
2013-09-19 21:51:49 +00:00
|
|
|
{
|
|
|
|
if (stack->cur_size == stack->max_size) {
|
|
|
|
stack->max_size *= 2;
|
2013-10-11 15:04:04 +00:00
|
|
|
xrealloc(stack->arr, stack->max_size);
|
2013-09-19 21:51:49 +00:00
|
|
|
}
|
|
|
|
stack->arr[stack->cur_size++] = elt;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:10:20 +00:00
|
|
|
void* pop(stack_t* stack)
|
2013-09-19 21:51:49 +00:00
|
|
|
{
|
2013-10-11 21:10:20 +00:00
|
|
|
void* res = stack->arr[--stack->cur_size];
|
|
|
|
return res;
|
2013-09-19 21:51:49 +00:00
|
|
|
}
|