blob: d13749e0e498185af2732d2af55b949ffe475a5b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include "stack.h"
#include "structs.h"
#include "error.h"
#include "curses.h" /* Required by error.h */
#include "stdio.h" /* Required by error.h */
PositionStack ps_new(void) {
PositionStack ps;
ps.capacity = STACK_INITIAL_CAPACITY;
ps.arr = malloc(sizeof(Position) * ps.capacity);
ps.top = 0;
return ps;
}
int ps_push(PositionStack *ps, Position pos) {
if (ps->top >= ps->capacity) {
ps->capacity *= STACK_SIZE_COEFFICIENT;
if ((ps->arr = realloc(ps->arr, sizeof(Position) * ps->capacity)) == NULL) error("Failed to realloc ps->arr\n");
}
ps->arr[ps->top] = pos;
ps->top += 1;
return 0;
}
Position ps_pop(PositionStack *ps) {
if (ps->top == 0) error("Stack underflow\n");
ps->top -= 1;
return ps->arr[ps->top];
}
Position ps_peek(PositionStack ps) {
if (ps.top == 0) error("Stack underflow\n");
return ps.arr[ps.top - 1];
}
void ps_free(PositionStack ps) {
free(ps.arr);
}
|